How to redirect to different url based on roles in symfony 2

后端 未结 6 1170
梦谈多话
梦谈多话 2020-12-02 17:37

I have one login page on site. I have 4 different tye of users and i want that when they login they go to different page based on their role assigned.

Is there any w

6条回答
  •  星月不相逢
    2020-12-02 18:26

    I used Mdrollette answer but this solution has a big drawback, you completely override the symfony original response and by doing this remove the remember me cookie that was set in the header by symfony.

    my solution was to change the OnKernelResponse this way :

    public function onKernelResponse(FilterResponseEvent $event)
    {
        if ($this->security->isGranted('ROLE_TEAM')) {
            $event->getResponse()->headers->set('Location', $this->router->generate('team_homepage'));    
        } elseif ($this->security->isGranted('ROLE_VENDOR')) {
            $event->getResponse()->headers->set('Location', $this->router->generate('vendor_homepage'));
        } else {
            $event->getResponse()->headers->set('Location', $this->router->generate('homepage'));
        }
    }
    

    This way you remain the remember me cookie intact.

提交回复
热议问题