15

I have provided a pre build project on symfony in which the logout session redirects to the login screen, but now I want that page to redirect on the home page instead. What I have found in the coding files is this:

In the base twig file:

<a href="{{path('log_out')}}"><i class="icon-key"></i> Log Out</a>

In routing.yml

#Route for logout page.
log_out:
    pattern: /bid/logout
yivi
  • 42,438
  • 18
  • 116
  • 138
Geetika
  • 790
  • 3
  • 13
  • 29

3 Answers3

27

Normally it is redirect to home. Check your security.yml config file.

firewalls:        
    default:            
        logout:
            path:   /logout
            target: / #This is home url
Dinuka Thilanga
  • 4,220
  • 10
  • 56
  • 93
3

On Symfony 4, by default, the logout action redirects to the / path. You can change the default behavior by setting the proper configuration parameter in the security.yml config file.

security:
    ...
    firewalls:
        ...
        main:
            ...
            logout:
                ...
                target: app_login # you can specify a hard coded URL path or a route name
Dayron Gallardo
  • 1,502
  • 2
  • 21
  • 37
1

The easiest way, to my humble opinion, is to simply do a redirection in your logoutAction (so in your controller), like this :

public function myLogoutAction()
{
    // Your logout logic
    return $this->redirect($this->generateUrl('my_route'));
}
Guillaume Fache
  • 813
  • 10
  • 21
  • i made it work by changing the pattern to /home. But i want to know is it a right approach to do this? – Geetika May 27 '15 at 08:09
  • If you change the pattern to `/home`, you will go to the home page, but you will not call your logoutAction, which is I believe what is wanted. So no, it is not the good way to do this. If you want to do a redirection without loging out, donc change the pattern, but the href in your link. – Guillaume Fache May 27 '15 at 08:15
  • i found this in the logout function of controller: `public function logoutAction() { throw new \RuntimeException('Please Configure the logout in your security setting.'); }` How to edit link here? – Geetika May 27 '15 at 08:30
  • Don't get confused : either you change the href in the link of your base twig file, the first piece of code you put (BUT YOU WON'T HAVE A REAL LOGOUT, JUST A REDIRECTION), OR you put at the end of your action the line of my answer. But that was guessing you had a logoutAction written. It seems from the Exception you posted that there is no logout action at all configured, so you will have to do that if you want to have a real logout (and if you are a newbie like you said you are, it will not be easy) – Guillaume Fache May 27 '15 at 08:36