`, make sure
+you have imported the logout route loader in your routes:
- class SecurityController extends AbstractController
- {
- #[Route('/logout', name: 'app_logout', methods: ['GET'])]
- public function logout(): never
- {
- // controller can be blank: it will never be called!
- throw new \Exception('Don\'t forget to activate logout in security.yaml');
- }
- }
+.. configuration-block::
.. code-block:: yaml
- # config/routes.yaml
- app_logout:
- path: /logout
- methods: GET
+ # config/routes/security.yaml
+ _symfony_logout:
+ resource: security.route_loader.logout
+ type: service
.. code-block:: xml
-
+
-
+
.. code-block:: php
- // config/routes.php
+ // config/routes/security.php
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
- return function (RoutingConfigurator $routes): void {
- $routes->add('app_logout', '/logout')
- ->methods(['GET'])
- ;
+ return static function (RoutingConfigurator $routes): void {
+ $routes->import('security.route_loader.logout', 'service');
};
-That's it! By sending a user to the ``app_logout`` route (i.e. to ``/logout``)
-Symfony will un-authenticate the current user and redirect them.
+.. versionadded:: 6.4
+
+ The :class:`Symfony\\Bundle\\SecurityBundle\\Routing\\LogoutRouteLoader` was
+ introduced in Symfony 6.4.
Logout programmatically
~~~~~~~~~~~~~~~~~~~~~~~
@@ -1989,6 +1978,105 @@ to execute custom logic::
}
}
+Customizing Logout Path
+~~~~~~~~~~~~~~~~~~~~~~~
+
+Another option is to configure ``path`` as a route name. This can be useful
+if you want logout URIs to be dynamic (e.g. translated according to the
+current locale). In that case, you have to create this route yourself:
+
+.. configuration-block::
+
+ .. code-block:: yaml
+
+ # config/routes.yaml
+ app_logout:
+ path:
+ en: /logout
+ fr: /deconnexion
+ methods: GET
+
+ .. code-block:: xml
+
+
+
+
+
+
+ /logout
+ /deconnexion
+
+
+
+ .. code-block:: php
+
+ // config/routes.php
+ use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
+
+ return function (RoutingConfigurator $routes): void {
+ $routes->add('app_logout', [
+ 'en' => '/logout',
+ 'fr' => '/deconnexion',
+ ])
+ ->methods(['GET'])
+ ;
+ };
+
+Then, pass the route name to the ``path`` option:
+
+.. configuration-block::
+
+ .. code-block:: yaml
+
+ # config/packages/security.yaml
+ security:
+ # ...
+
+ firewalls:
+ main:
+ # ...
+ logout:
+ path: app_logout
+
+ .. code-block:: xml
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ .. code-block:: php
+
+ // config/packages/security.php
+ use Symfony\Config\SecurityConfig;
+
+ return static function (SecurityConfig $security): void {
+ // ...
+
+ $mainFirewall = $security->firewall('main');
+ // ...
+ $mainFirewall->logout()
+ ->path('app_logout')
+ ;
+ };
+
.. _retrieving-the-user-object:
Fetching the User Object
pFad - Phonifier reborn
Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies:
Alternative Proxy
pFad Proxy
pFad v3 Proxy
pFad v4 Proxy