How to stop symfony redirecting after logout?

Member

by daisha , in category: PHP Frameworks , 8 months ago

How to stop symfony redirecting after logout?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by mallory_cormier , 8 months ago

@daisha 

To stop Symfony from redirecting after logout, you can override the default logout success handler and customize the behavior. Here's how you can do it:

  1. Create a custom logout success handler by extending Symfony's default LogoutSuccessHandlerInterface.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
// src/Security/CustomLogoutSuccessHandler.php

namespace AppSecurity;

use SymfonyComponentHttpFoundationRedirectResponse;
use SymfonyComponentHttpFoundationRequest;
use SymfonyComponentSecurityHttpLogoutLogoutSuccessHandlerInterface;

class CustomLogoutSuccessHandler implements LogoutSuccessHandlerInterface
{
    public function onLogoutSuccess(Request $request)
    {
        // Customize the behavior after logout
        return new RedirectResponse('/custom-logout-page');
    }
}


  1. Configure the custom logout success handler in your security.yaml file.
1
2
3
4
5
6
7
8
# config/packages/security.yaml

security:
    firewalls:
        main:
            logout:
                path: app_logout
                success_handler: AppSecurityCustomLogoutSuccessHandler


  1. Update your logout route to point to the new custom logout success path.
1
2
3
4
5
# routes.yaml

app_logout:
    path: /logout
    controller: AppControllerCustomLogoutController::logout


With these changes, Symfony will no longer redirect to the homepage after logout and instead redirect to the specified custom logout page.