src/EventListener/LogoutSubscriber.php line 23

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Symfony\Component\HttpFoundation\RedirectResponse;
  5. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  6. use Symfony\Component\Security\Http\Event\LogoutEvent;
  7. class LogoutSubscriber implements EventSubscriberInterface
  8. {
  9.     private UrlGeneratorInterface $urlGenerator;
  10.     public function __construct(
  11.         UrlGeneratorInterface $urlGenerator
  12.     ) {
  13.         $this->urlGenerator $urlGenerator;
  14.     }
  15.     public static function getSubscribedEvents(): array
  16.     {
  17.         return [LogoutEvent::class => 'onLogout'];
  18.     }
  19.     public function onLogout(LogoutEvent $event): void
  20.     {
  21.         // get the security token of the session that is about to be logged out
  22.         $token $event->getToken();
  23.         // dump($token); exit;
  24.         // get the current request
  25.         $request $event->getRequest();
  26.         // get the current response, if it is already set by another listener
  27.         $response $event->getResponse();
  28.         // configure a custom logout response to the homepage
  29.         $response = new RedirectResponse(
  30.             $this->urlGenerator->generate('homepage'),
  31.             RedirectResponse::HTTP_SEE_OTHER
  32.         );
  33.         $event->setResponse($response);
  34.     }
  35. }
  36. ?>