登录后从登录页面重定向FOSUserBundle

问题描述:

我只是希望管理员用户或前端用户即使登录后仍尝试访问登录页面

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)

然后应将其重定向回相关的主页,例如/admin/

then they should be redirected back to their related home page like /admin or /

您可以覆盖

You can override FOSUserBundle\Controller\SecurityController and add the following code at the top of loginAction.

use Symfony\Component\HttpFoundation\RedirectResponse;

// ...

public function loginAction(Request $request)
{
    $authChecker = $this->container->get('security.authorization_checker');
    $router = $this->container->get('router');

    if ($authChecker->isGranted('ROLE_ADMIN')) {
        return new RedirectResponse($router->generate('admin_home'), 307);
    } 

    if ($authChecker->isGranted('ROLE_USER')) {
        return new RedirectResponse($router->generate('user_home'), 307);
    }

    // ...