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

后端 未结 6 1169
梦谈多话
梦谈多话 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:34

    For the sake of testing, if you're wanting to to preserve the original response you could also just copy the headers. The clone method on the Redirect object only copies the headers.

    public function onKernelResponse(FilterResponseEvent $event)
    {
        if ($this->security->isGranted('ROLE_TEAM')) {
            $response = new RedirectResponse($this->router->generate('team_homepage'));
        } elseif ($this->security->isGranted('ROLE_VENDOR')) {
            $response = new RedirectResponse($this->router->generate('vendor_homepage'));
        } else {
            $response = new RedirectResponse($this->router->generate('homepage'));
        }
    
        $response->headers = $response->headers + $event->getResponse()->headers;
    
        $event->setResponse($response);
    }
    

提交回复
热议问题