FOSuserBundle中的DeleteUser方法symfony 2用户界面错误

时间:2015-03-16 19:31:12

标签: php symfony fosuserbundle

我在使用$ UserManager-> deleteUser($ user)删除用户时遇到问题。我收到错误:错误:类AppBundle \ Controller \ UsermanagerController包含35个抽象方法,因此必须声明为抽象方法或实现其余方法。

我的控制器:

namespace AppBundle\Controller;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use AppBundle\Entity\UserManager;
use FOS\UserBundle\Model\UserInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

class UsermanagerController extends Controller implements UserInterface
{
public function deleteAction($user){
$UserManager = $this->container->get('fos_user.user_manager');
if ($user ==  null) {
        throw new NotFoundHttpException('User not found for user ' . $user);
    }
$UserManager->deleteUser($user);
return $this->redirect($this->generateUrl('admin_index'));

}

我的实体:

namespace AppBundle/Entity;
use Doctrine\ORM\Mapping as ORM;
use FOS\UserBundle\Doctrine\UserManager as BaseCustomer;
use FOS\UserBundle\Model\UserInterface;

/**
*@ORM|Entitiy
*
*/
class UserManager extends BaseCustomer implements UserInterface
{

}

我的config.yml:  ORM:

resolve_target_entities:
    FOS\UserBundle\Model\UserInterface: AppBundle\Entity\UserManager

和Routing.yml:

fos_deleteuser_group:
    path: /app/usermanager/delete/{user}
    defaults: { _controller: AppBundle:Usermanager:delete }

我尝试使用FOS / UserBundle / doctrine / UserManager,没有运气。

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

总结:

您的第一个错误是控制器中的implements UserInterface。你已经把它删除了。

然后您的控制器内部出现了另一个问题,因为您将字符串传递给UserManager::deleteUser方法,该方法需要一些UserInterface

我会为你提供一些代码,应该解决这个问题。

<?php
/**
 * @param $username string
 * @return Response
 */
public function deleteAction($username) {
  $userManager = $this->get('fos_user.user_manager');
  /* @var $userManager UserManager */

  $user = $userManager->findUserByUsername($username);
  if(\is_null($user)) {
    // user not found, generate $notFoundResponse
    return $notFoundResponse;
  }

  \assert(!\is_null($user));
  $userManager->deleteUser($user);

  // okay, generate $okayResponse
  return $okayResponse;
}