Call to a member function get() on null for FOSuserBundle

最后都变了- 提交于 2019-12-25 09:19:55

问题


I am trying to do a redirect to the registration page, if a username does not exists in the user tables. I have done all the configurations needed and I have created the handler that looks like:

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);
        }
    }
}

For this part of the code I am getting the error:

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

Why is this happening?

Edit: I initialized correcly constructor. I inserted the code: else { $url=$this->container->get('router')->generate('fos_user_sec‌​urity_login'); return new RedirectResponse($url); } but it just redirects me without the login errors

来源:https://stackoverflow.com/questions/41539822/call-to-a-member-function-get-on-null-for-fosuserbundle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!