Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

symfony 2 redirect if user is logged in and wants to call /login

Tags:

php

symfony

I've the following problem in my symfony 2 application.

When a user is already is logged in he still has access to the route /login. What is the best way to avoid this case ?

I tried to redirect the user if he wants to login, but this didn't works, as i thought.

public function loginAction()
{
   if (!$this->get('security.context')->isGranted('ROLE_USER')) {
       // not logged in
       ...
       code
       ...
       ..
       .
    } else {
        // already logged in 
        $this->redirect($this->generateUrl('HereIsMyRouteName'));
    }
}
like image 298
Nextar Avatar asked Dec 06 '25 13:12

Nextar


1 Answers

I solved it on my own, the problem was that I have to return the redirect.

return $this->redirect($this->generateUrl('HereIsMyRouteName'));
like image 173
Nextar Avatar answered Dec 08 '25 03:12

Nextar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!