Skip to content

Commit a7baa04

Browse files
Adding full subscriber example
Reason: It's not so easy to figure this out, since the linked Event Subscriber page doesn't show how to subscribe to this specific event. Questions: * The introduction text says "e.g. invalidate some tokens". How can this be done? * How can you add a flash message? `$this->addFlashMessage()` didn't work for me. * I'm extending `AbstractController` to have access to `$this->generateUrl()` - is this the easiest/best way?
1 parent 2472528 commit a7baa04

File tree

1 file changed

+38
-16
lines changed

1 file changed

+38
-16
lines changed

security.rst

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1154,18 +1154,42 @@ In some cases you need to execute extra logic upon logout (e.g. invalidate
11541154
some tokens) or want to customize what happens after a logout. During
11551155
logout, a :class:`Symfony\\Component\\Security\\Http\\Event\\LogoutEvent`
11561156
is dispatched. Register an :doc:`event listener or subscriber </event_dispatcher>`
1157-
to execute custom logic. The following information is available in the
1158-
event class:
1157+
to execute custom logic::
11591158

1160-
``getToken()``
1161-
Returns the security token of the session that is about to be logged
1162-
out.
1163-
``getRequest()``
1164-
Returns the current request.
1165-
``getResponse()``
1166-
Returns a response, if it is already set by a custom listener. Use
1167-
``setResponse()`` to configure a custom logout response.
1159+
// src/EventListener/LogoutSubscriber.php
1160+
namespace App\EventListener;
11681161

1162+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
1163+
use Symfony\Component\Security\Http\Event\LogoutEvent;
1164+
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
1165+
use Symfony\Component\HttpFoundation\RedirectResponse;
1166+
1167+
class LogoutSubscriber extends AbstractController implements EventSubscriberInterface
1168+
{
1169+
public static function getSubscribedEvents()
1170+
{
1171+
// return the subscribed events, their methods and priorities
1172+
return [
1173+
LogoutEvent::class => ['logout', 0],
1174+
];
1175+
}
1176+
1177+
public function logout(LogoutEvent $event)
1178+
{
1179+
// Returns the security token of the session that is about to be logged out:
1180+
$event->getToken();
1181+
1182+
// Returns the current request:
1183+
$request = $event->getRequest();
1184+
1185+
// Returns a response, if it is already set by a custom listener:
1186+
$response = $event->getResponse();
1187+
1188+
// Configure a custom logout response:
1189+
$response = new RedirectResponse($this->generateUrl('homepage', []), RedirectResponse::HTTP_SEE_OTHER);
1190+
$event->setResponse($response);
1191+
}
1192+
}
11691193

11701194
.. tip::
11711195

@@ -1185,7 +1209,7 @@ event class:
11851209
services:
11861210
# ...
11871211
1188-
App\EventListener\CustomLogoutSubscriber:
1212+
App\EventListener\LogoutSubscriber:
11891213
tags:
11901214
- name: kernel.event_subscriber
11911215
dispatcher: security.event_dispatcher.main
@@ -1202,7 +1226,7 @@ event class:
12021226
<services>
12031227
<!-- ... -->
12041228
1205-
<service id="App\EventListener\CustomLogoutSubscriber">
1229+
<service id="App\EventListener\LogoutSubscriber">
12061230
<tag name="kernel.event_subscriber"
12071231
dispacher="security.event_dispatcher.main"
12081232
/>
@@ -1215,14 +1239,12 @@ event class:
12151239
// config/services.php
12161240
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
12171241
1218-
use App\EventListener\CustomLogoutListener;
1219-
use App\EventListener\CustomLogoutSubscriber;
1220-
use Symfony\Component\Security\Http\Event\LogoutEvent;
1242+
use App\EventListener\LogoutSubscriber;
12211243
12221244
return function(ContainerConfigurator $configurator) {
12231245
$services = $configurator->services();
12241246
1225-
$services->set(CustomLogoutSubscriber::class)
1247+
$services->set(LogoutSubscriber::class)
12261248
->tag('kernel.event_subscriber', [
12271249
'dispatcher' => 'security.event_dispatcher.main',
12281250
]);

0 commit comments

Comments
 (0)