How to redirect to home page after logout?
Asked Answered
P

3

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
Pouliot answered 27/5, 2015 at 7:55 Comment(0)
M
27

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

firewalls:        
    default:            
        logout:
            path:   /logout
            target: / #This is home url
Mantoman answered 27/5, 2015 at 8:55 Comment(1)
my target id blank...i.e /Pouliot
E
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
Excreta answered 17/7, 2019 at 13:45 Comment(0)
A
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'));
}
Aristaeus answered 27/5, 2015 at 8:2 Comment(4)
i made it work by changing the pattern to /home. But i want to know is it a right approach to do this?Pouliot
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.Aristaeus
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?Pouliot
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)Aristaeus

© 2022 - 2024 — McMap. All rights reserved.