src/EventSubscribers/LogoutSubscriber.php line 36
<?phpnamespace App\EventSubscribers;use App\Repository\SessionRepository;use App\Repository\UserRepository;use DateTimeImmutable;use Symfony\Bundle\SecurityBundle\Security;use Symfony\Component\EventDispatcher\EventSubscriberInterface;use Symfony\Component\HttpFoundation\RedirectResponse;use Symfony\Component\HttpFoundation\Response;use Symfony\Component\Routing\Generator\UrlGeneratorInterface;use Symfony\Component\Security\Http\Event\LogoutEvent;use Symfony\Component\Security\Http\Firewall\LogoutListener;class LogoutSubscriber implements EventSubscriberInterface{private UserRepository $userRepository;private $security;/*** @param UrlGeneratorInterface $urlGenerator* @param UserRepository $userRepository*/public function __construct(private UrlGeneratorInterface $urlGenerator, UserRepository $userRepository, Security $security){$this->userRepository = $userRepository;$this->security = $security;}public static function getSubscribedEvents(): array{return [LogoutEvent::class => 'onLogout'];}public function onLogout(LogoutEvent $event): void{$user = $this->security->getUser();// get the security token of the session that is about to be logged out$token = $event->getToken();// get the current request$request = $event->getRequest();// get the current response, if it is already set by another listener$response = $event->getResponse();// configure a custom logout response to the homepage$response = new RedirectResponse($this->urlGenerator->generate('app_home'),Response::HTTP_SEE_OTHER);$event->setResponse($response);}}