为FOSuserBundle调用null的成员函数get()

时间:2017-01-09 01:36:37

标签: symfony login interface

如果用户表中不存在用户名,我正在尝试重定向到注册页面。我已经完成了所需的所有配置,并且我创建了一个看起来像这样的处理程序:

namespace UserBundle\Redirection;

use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface;

class AfterLoginFailureRedirection implements AuthenticationFailureHandlerInterface
{
    /**
     * @var \Symfony\Component\Routing\RouterInterface
     */
    private $router;

    /**
     * @var \Symfony\Component\DependencyInjection\ContainerInterface
     */
    private $container;

    /**
     * AfterLoginFailureRedirection constructor.
     * @param RouterInterface $router
     * @param ContainerInterface $container
     */
    public function __construct(RouterInterface $router, ContainerInterface $container)
    {
        $this->router = $router;
    }

    /**
     * @param Request $request
     * @param AuthenticationException $token
     * @return RedirectResponse
     */
    public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
    {
        //$request = $this->container->get('request');
        $token=$exception->getToken();
        $username=$token->getUsername();
        //$username = $request->request->get('email');
        $user = $this->container->get('fos_user.user_manager')->findUserByUsername($username);

        if(!$user->isUser()) {
            $url=$this->container->get('router')->generate('fos_user_registration_register');
            return new RedirectResponse($url);
        }
    }
}

对于这部分代码,我收到错误:

$user = $this->container->get('fos_user.user_manager')->findUserByUsername($username);

为什么会这样?

编辑: 我初始化了correcly构造函数。 我插入了代码:else {$ url = $ this-> container-> get('router') - > generate('fos_user_sec urity_login');返回新的RedirectResponse($ url);但它只是重定向我没有登录错误

0 个答案:

没有答案