i am trying to implement a redirect after login in my symfony2 application in order to redirect if my user has one attribute or not. I've created the class AuthenticationSuccessHandler.php inside Handler Folder in my project:
namespace Me\MyBundle\Handler;
use Symfony\Component\Security\Http\HttpUtils;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Http\Authentication\DefaultAuthenticationSuccessHandler;
class AuthenticationSuccessHandler extends DefaultAuthenticationSuccessHandler {
public function __construct( HttpUtils $httpUtils, array $options ) {
parent::__construct( $httpUtils, $options );
}
public function onAuthenticationSuccess( Request $request, TokenInterface $token ) {
$user = $token->getUser();
if($user->getProfile()!=1){
$url = 'fos_user_profile_edit';
}else{
$url = 'My_route';
}
return new RedirectResponse($this->router->generate($url));
}
}
But when i log in, i get an error:
Notice: Undefined property: Me\MyBundle\Handler\AuthenticationSuccessHandler::$router in /var/www/MyBundle/src/Me/MyBundle/Handler/AuthenticationSuccessHandler.php line 28
The error is in happening in the "return new RedirectResponse($this->router->generate($url));"
I also have my service:
my_auth_success_handler:
class: Me\MyBundle\Handler\AuthenticationSuccessHandler
public: false
arguments: [ @security.http_utils, [] ]
and the success handler in the security.yml:
fos_facebook:
success_handler: my_auth_success_handler
Any ideas? Thank you very much.
You have no
@routerservice injected. Modify your constructorAnd service definition: