问题
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_security_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