php - How to stop Symfony redirecting after logout

one text

Solution:

You can write a custom logout handler. Actually, there is a new approach for this, introduced in symfony 5.1. Basically, now you can register an event listener either globally or for some specific firewall, and perform any actions after the person has logged out.

Returning to your problem (from the blog post below):

The Symfony\Component\Security\Http\Event\LogoutEvent object received by the listener contains useful methods such as getToken() (to get the security token of the session), getRequest() and setResponse().

The later will help you. It means you can return anything you want instead of default RedirectResponse by setting new response object to the event.

services.yaml:

services:
    App\EventListener\CustomLogoutListener:
        tags:
            - name: 'kernel.event_listener'
              event: 'Symfony\Component\Security\Http\Event\LogoutEvent'
              dispatcher: security.event_dispatcher.main
              method: onLogout

And your listener:

namespace App\EventListener;

use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Security\Http\Event\LogoutEvent;

class CustomLogoutListener
{
    public function onLogout(LogoutEvent $logoutEvent): void
    {
        $logoutEvent->setResponse(new JsonResponse([]));
    }
}

Read more: Simpler logout customization

Source