Skip to content

Commit 4648d02

Browse files
committed
minor #15186 Adding full subscriber example (ThomasLandauer)
This PR was submitted for the 5.2 branch but it was merged into the 5.4 branch instead. Discussion ---------- 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->addFlash()` didn't work for me. * I'm extending `AbstractController` to have access to `$this->generateUrl()` - is this the easiest/best way? Commits ------- 4f90b3c Adding full subscriber example
2 parents 5622342 + 4f90b3c commit 4648d02

File tree

1 file changed

+38
-17
lines changed

1 file changed

+38
-17
lines changed

security.rst

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1766,17 +1766,40 @@ In some cases you need to run extra logic upon logout (e.g. invalidate
17661766
some tokens) or want to customize what happens after a logout. During
17671767
logout, a :class:`Symfony\\Component\\Security\\Http\\Event\\LogoutEvent`
17681768
is dispatched. Register an :doc:`event listener or subscriber </event_dispatcher>`
1769-
to run custom logic. The following information is available in the
1770-
event class:
1771-
1772-
``getToken()``
1773-
Returns the security token of the session that is about to be logged
1774-
out.
1775-
``getRequest()``
1776-
Returns the current request.
1777-
``getResponse()``
1778-
Returns a response, if it is already set by a custom listener. Use
1779-
``setResponse()`` to configure a custom logout response.
1769+
to execute custom logic::
1770+
1771+
// src/EventListener/LogoutSubscriber.php
1772+
namespace App\EventListener;
1773+
1774+
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
1775+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
1776+
use Symfony\Component\HttpFoundation\RedirectResponse;
1777+
use Symfony\Component\Security\Http\Event\LogoutEvent;
1778+
1779+
class LogoutSubscriber extends AbstractController implements EventSubscriberInterface
1780+
{
1781+
public static function getSubscribedEvents(): array
1782+
{
1783+
return [LogoutEvent::class => 'onLogout'];
1784+
}
1785+
1786+
public function onLogout(LogoutEvent $event): void
1787+
{
1788+
// get the security token of the session that is about to be logged out
1789+
$token = $event->getToken();
1790+
1791+
// get the current request
1792+
$request = $event->getRequest();
1793+
1794+
// get the current response, if it is already set by another listener
1795+
$response = $event->getResponse();
1796+
1797+
// configure a custom logout response
1798+
$event->setResponse(
1799+
new RedirectResponse($this->generateUrl('homepage', []), RedirectResponse::HTTP_SEE_OTHER)
1800+
);
1801+
}
1802+
}
17801803

17811804
.. _retrieving-the-user-object:
17821805

@@ -2534,7 +2557,7 @@ for these events.
25342557
services:
25352558
# ...
25362559
2537-
App\EventListener\CustomLogoutSubscriber:
2560+
App\EventListener\LogoutSubscriber:
25382561
tags:
25392562
- name: kernel.event_subscriber
25402563
dispatcher: security.event_dispatcher.main
@@ -2551,7 +2574,7 @@ for these events.
25512574
<services>
25522575
<!-- ... -->
25532576
2554-
<service id="App\EventListener\CustomLogoutSubscriber">
2577+
<service id="App\EventListener\LogoutSubscriber">
25552578
<tag name="kernel.event_subscriber"
25562579
dispatcher="security.event_dispatcher.main"
25572580
/>
@@ -2564,14 +2587,12 @@ for these events.
25642587
// config/services.php
25652588
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
25662589
2567-
use App\EventListener\CustomLogoutListener;
2568-
use App\EventListener\CustomLogoutSubscriber;
2569-
use Symfony\Component\Security\Http\Event\LogoutEvent;
2590+
use App\EventListener\LogoutSubscriber;
25702591
25712592
return function(ContainerConfigurator $configurator) {
25722593
$services = $configurator->services();
25732594
2574-
$services->set(CustomLogoutSubscriber::class)
2595+
$services->set(LogoutSubscriber::class)
25752596
->tag('kernel.event_subscriber', [
25762597
'dispatcher' => 'security.event_dispatcher.main',
25772598
]);

0 commit comments

Comments
 (0)