This is the how to for redirection implementation by roles after login or logout in Symfony2
- Copy
AfterLoginRedirection.php
andAfterLogoutRedirection.php
to your bundle under the "Redirection" directory - Edit your
services.yml
file in your bundle and copy/paste the content ofservices.yml
- Edit your
security.yml
file... - ... add
success_handler: redirect.after.login
inform_login:
section - ... add
success_handler: redirect.after.logout
inlogout:
section
For more informations, you can read my explanation on my blog (in french only)
@notabigboy:
1- An user have many roles. In this code the redirection is make depending the principal role of the user
for example the list roles of the user is
The principal role here is "ROLE_ADMIN"
2-The role is an attribute of the user entity. So no relationship context here.
In case, if you want to have separate roles for each user, you can create Role entity, that should implementing Symfony\Component\Security\Core\Role\RoleInterface