@@ -899,11 +899,93 @@ Next, you'll need to create a route for this URL (but not a controller):
899899 And that's it! By sending a user to the ``app_logout `` route (i.e. to ``/logout ``)
900900Symfony will un-authenticate the current user and redirect them.
901901
902+ Customizing Logout
903+ ~~~~~~~~~~~~~~~~~~
904+
905+ .. versionadded :: 5.1
906+
907+ The ``LogoutEvent `` was introduced in Symfony 5.1. Prior to this
908+ version, you had to use a
909+ :ref: `logout success handler <reference-security-logout-success-handler >`
910+ to customize the logout.
911+
912+ In some cases you need to execute extra logic upon logout (e.g. invalidate
913+ some tokens) or want to customize what happens after a logout. During
914+ logout, a :class: `Symfony\\ Component\\ Security\\ Http\\ Event\\ LogoutEvent `
915+ is dispatched. Register an :doc: `event listener or subscriber </event_dispatcher >`
916+ to execute custom logic. The following information is available in the
917+ event class:
918+
919+ ``getToken() ``
920+ Returns the security token of the session that is about to be logged
921+ out.
922+ ``getRequest() ``
923+ Returns the current request.
924+ ``getResponse() ``
925+ Returns a response, if it is already set by a custom listener. Use
926+ ``setResponse() `` to configure a custom logout response.
927+
928+
902929.. tip ::
903930
904- Need more control of what happens after logout? Add a ``success_handler `` key
905- under ``logout `` and point it to a service id of a class that implements
906- :class: `Symfony\\ Component\\ Security\\ Http\\ Logout\\ LogoutSuccessHandlerInterface `.
931+ Every Security firewall has its own event dispatcher
932+ (``security.event_dispatcher.FIREWALLNAME ``). The logout event is
933+ dispatched on both the global and firewall dispatcher. You can register
934+ on the firewall dispatcher if you want your listener to only be
935+ executed for a specific firewall. For instance, if you have an ``api ``
936+ and ``main `` firewall, use this configuration to register only on the
937+ logout event in the ``main `` firewall:
938+
939+ .. configuration-block ::
940+
941+ .. code-block :: yaml
942+
943+ # config/services.yaml
944+ services :
945+ # ...
946+
947+ App\EventListener\CustomLogoutSubscriber :
948+ tags :
949+ - name : kernel.event_subscriber
950+ dispacher : security.event_dispatcher.main
951+
952+ .. code-block :: xml
953+
954+ <!-- config/services.xml -->
955+ <?xml version =" 1.0" encoding =" UTF-8" ?>
956+ <container xmlns =" http://symfony.com/schema/dic/services"
957+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
958+ xsi : schemaLocation =" http://symfony.com/schema/dic/services
959+ https://symfony.com/schema/dic/services/services-1.0.xsd" >
960+
961+ <services >
962+ <!-- ... -->
963+
964+ <service id =" App\EventListener\CustomLogoutSubscriber" >
965+ <tag name =" kernel.event_subscriber"
966+ dispacher =" security.event_dispatcher.main"
967+ />
968+ </service >
969+ </services >
970+ </container >
971+
972+ .. code-block :: php
973+
974+ // config/services.php
975+ namespace Symfony\Component\DependencyInjection\Loader\Configurator;
976+
977+ use App\EventListener\CutomLogoutListener;
978+ use App\EventListener\CutomLogoutSubscriber;
979+ use Symfony\Component\Security\Http\Event\LogoutEvent;
980+
981+ return function(ContainerConfigurator $configurator) {
982+ $services = $configurator->services();
983+
984+ $services->set(CustomLogoutSubscriber::class)
985+ ->tag('kernel.event_subscriber', [
986+ 'dispatcher' => 'security.event_dispatcher.main',
987+ ]);
988+ };
907989
908990 .. _security-role-hierarchy :
909991
0 commit comments