Change logout success handler to an event listener that listens to LogoutEvent
-use Symfony\Component\Security\Http\Logout\LogoutSuccessHandlerInterface;
-use Symfony\Component\HttpFoundation\Request;
-use Symfony\Component\HttpFoundation\Response;
-use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
+use Symfony\Component\EventDispatcher\EventSubscriberInterface;
+use Symfony\Component\Security\Http\Event\LogoutEvent;
-final class SomeLogoutHandler implements LogoutSuccessHandlerInterface
+final class SomeLogoutHandler implements EventSubscriberInterface
{
/**
* @var HttpUtils
*/
private $httpUtils;
- public function __construct(HttpUtils $httpUtils)
+ public function onLogout(LogoutEvent $logoutEvent): void
{
- $this->httpUtils = $httpUtils;
+ if ($logoutEvent->getResponse() !== null) {
+ return;
+ }
+
+ $response = $this->httpUtils->createRedirectResponse($logoutEvent->getRequest(), 'some_url');
+ $logoutEvent->setResponse($response);
}
- public function onLogoutSuccess(Request $request)
+ /**
+ * @return array<string, mixed>
+ */
+ public static function getSubscribedEvents(): array
{
- $response = $this->httpUtils->createRedirectResponse($request, 'some_url');
- return $response;
+ return [
+ LogoutEvent::class => [['onLogout', 64]],
+ ];
}
}
Configure your rector.php
:
<?php
use Rector\Config\RectorConfig;
use Rector\Symfony\Symfony51\Rector\Class_\LogoutSuccessHandlerToLogoutEventSubscriberRector;
return RectorConfig::configure()
->withRules([
LogoutSuccessHandlerToLogoutEventSubscriberRector::class,
]);