I simply want that if admin user or front end user try to access login page even after logged in
/admin/login (admin user)
OR
/login (front end user)
then they should be redirected back to their related home page like /admin
or /
I simply want that if admin user or front end user try to access login page even after logged in
/admin/login (admin user)
OR
/login (front end user)
then they should be redirected back to their related home page like /admin
or /
Redirecting on login/logout in Symfony2 using LoginHandlers
You should implement the
AuthenticationSuccessHandlerInterface
to handle the last minute decision when the login success.Implement the AuthenticationSuccessHandlerInterface:
Register your class as a service:
Add a reference to your LoginSuccessHandler class in the firewall
You can override FOSUserBundle\Controller\SecurityController and add the following code at the top of
loginAction
.Just redirect in the controller of the page that you added in
default_target_path
to the wanted direction, for example, if you put fordefault_target_path: /index
and index is an action defined inHomePageCOntroller
, go toHomePageCOntroller
, test if the current user is admin or not with:and then redircet him to the admin space.
The easier solution is to add these two lines to your app/config/security.yml:
always_use_default_target_path & default_target_path, e.g.: