I am trying to redirect the route based on the Role after registration / login. After the registration, I am redirecting the route to this secure_area, to route according to the user roles.
/**
* @Route("/secure", name="secure_area")
*
* @throws \Exception
*/
public function indexAction()
{
if ($this->isGranted('ROLE_USER1')) {
return $this->redirectToRoute('user1');
}
if ($this->isGranted('ROLE_USER2')) {
return $this->redirectToRoute('user2');
}
throw new \Exception(AccessDeniedException::class);
}
In both cases, I am landing to the route user1. How can I make it redirect the route according to the user roles?
security.yaml
role_hierarchy:
ROLE_ADMIN: ROLE_USER2
ROLE_USER2: ROLE_USER1
ROLE_USER1: ROLE_USER1
access_control:
- { path: ^/admin, roles: ROLE_ADMIN }
- { path: ^/user2, roles: ROLE_USER2 }
- { path: ^/user1, roles: ROLE_USER1 }