基于用户角色的登录后重定向
Redirection after login based on user role
我在 Symfony 3.4 中使用 FosUserBundle。我想根据用户的角色重定向用户。例如,如果角色是客户,则用户将被重定向到客户页面。如果用户是管理员,则用户将被重定向到管理仪表板页面。我如何使用 FosUserBundle 执行此操作?
将它们都重定向到名为 indexAction()
的控制器,并根据角色在控制器中重定向它们。像这样:
/**
* @Route("/secure-area", name="homepage")
*/
public function indexAction()
{
if($this->getUser()->hasRole('ROLE_ADMIN'))
return $this->redirect($this->generateUrl('admin_area'));
elseif($this->getUser()->hasRole('ROLE_USER'))
return $this->redirect($this->generateUrl('client_area'));
throw new \Exception(AccessDeniedException::class);
}
编辑:
您应该将 default_target_path 设置为上面的路径
我在 Symfony 3.4 中使用 FosUserBundle。我想根据用户的角色重定向用户。例如,如果角色是客户,则用户将被重定向到客户页面。如果用户是管理员,则用户将被重定向到管理仪表板页面。我如何使用 FosUserBundle 执行此操作?
将它们都重定向到名为 indexAction()
的控制器,并根据角色在控制器中重定向它们。像这样:
/**
* @Route("/secure-area", name="homepage")
*/
public function indexAction()
{
if($this->getUser()->hasRole('ROLE_ADMIN'))
return $this->redirect($this->generateUrl('admin_area'));
elseif($this->getUser()->hasRole('ROLE_USER'))
return $this->redirect($this->generateUrl('client_area'));
throw new \Exception(AccessDeniedException::class);
}
编辑: 您应该将 default_target_path 设置为上面的路径