FOSUserBundle redirect from login page after logged in

前端 未结 4 820
北恋
北恋 2020-12-02 15:05

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



        
4条回答
  •  生来不讨喜
    2020-12-02 15:18

    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);
        }
    
        // ... 
    

提交回复
热议问题