8000 [Security] Document the `LogoutRouteLoader` · symfony/symfony-docs@715899b · GitHub
[go: up one dir, main page]

Skip to content

Commit 715899b

Browse files
committed
[Security] Document the LogoutRouteLoader
1 parent f1c0f13 commit 715899b

File tree

1 file changed

+55
-26
lines changed

1 file changed

+55
-26
lines changed

security.rst

Lines changed: 55 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1796,7 +1796,7 @@ To enable logging out, activate the ``logout`` config parameter under your fire
17961796
main:
17971797
# ...
17981798
logout:
1799-
path: app_logout
1799+
path: /logout
18001800
18011801
# where to redirect after logout
18021802
# target: app_any_route
@@ -1817,8 +1817,7 @@ To enable logging out, activate the ``logout`` config parameter under your fire
18171817
<!-- ... -->
18181818
18191819
<firewall name="main">
1820-
<!-- ... -->
1821-
<logout path="app_logout"/>
1820+
<logout path="/logout"/>
18221821
18231822
<!-- use "target" to configure where to redirect after logout
18241823
<logout path="app_logout" target="app_any_route"/>
@@ -1838,41 +1837,68 @@ To enable logging out, activate the ``logout`` config parameter under your fire
18381837
$mainFirewall = $security->firewall('main');
18391838
// ...
18401839
$mainFirewall->logout()
1841-
// the argument can be either a route name or a path
1842-
->path('app_logout')
1840+
->path('/logout')
18431841
18441842
// where to redirect after logout
18451843
// ->target('app_any_route')
18461844
;
18471845
};
18481846
1849-
Next, you need to create a route for this URL (but not a controller):
1847+
Symfony will then un-authenticate users navigating to the configured ``path``,
1848+
and redirect them to the configured ``target``. You can generate URLs to this
1849+
path using the ``_security_<firewallname>`` route name (e.g. ``_security_main``).
1850+
1851+
If your project does not use :ref:`Symfony Flex <symfony-flex>`, make sure
1852+
you have imported the logout route loader in your routes:
18501853

18511854
.. configuration-block::
18521855

1853-
.. code-block:: php-attributes
1856+
.. code-block:: yaml
18541857
1855-
// src/Controller/SecurityController.php
1856-
namespace App\Controller;
1858+
# config/routes/security.yaml
1859+
_symfony_logout:
1860+
resource: security.route_loader.logout
1861+
type: service
18571862
1858-
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
1859-
use Symfony\Component\Routing\Annotation\Route;
1863+
.. code-block:: xml
18601864
1861-
class SecurityController extends AbstractController
1862-
{
1863-
#[Route('/logout', name: 'app_logout', methods: ['GET'])]
1864-
public function logout(): never
1865-
{
1866-
// controller can be blank: it will never be called!
1867-
throw new \Exception('Don\'t forget to activate logout in security.yaml');
1868-
}
1869-
}
1865+
<!-- config/routes/security.xml -->
1866+
<?xml version="1.0" encoding="UTF-8" ?>
1867+
<routes xmlns="http://symfony.com/schema/routing"
1868+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1869+
xsi:schemaLocation="http://symfony.com/schema/routing
1870+
https://symfony.com/schema/routing/routing-1.0.xsd">
1871+
1872+
<import resource="security.route_loader.logout" type="service"/>
1873+
</routes>
1874+
1875+
.. code-block:: php
1876+
1877+
// config/routes/security.php
1878+
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
1879+
1880+
return static function (RoutingConfigurator $routes): void {
1881+
$routes->import('security.route_loader.logout', 'service');
1882+
};
1883+
1884+
.. versionadded:: 6.4
1885+
1886+
The :class:`Symfony\\Bundle\\SecurityBundle\\Routing\\LogoutRouteLoader` was
1887+
introduced in Symfony 6.4.
1888+
1889+
Another option is to configure ``path`` as a route name, which can be useful if
1890+
you want logout URIs to be translated according to the current locale e.g.
1891+
In that case, you have to create this route yourself:
1892+
1893+
.. configuration-block::
18701894

18711895
.. code-block:: yaml
18721896
18731897
# config/routes.yaml
18741898
app_logout:
1875-
path: /logout
1899+
path:
1900+
en: /logout
1901+
fr: /deconnexion
18761902
methods: GET
18771903
18781904
.. code-block:: xml
@@ -1884,7 +1910,10 @@ Next, you need to create a route for this URL (but not a controller):
18841910
xsi:schemaLocation="http://symfony.com/schema/routing
18851911
https://symfony.com/schema/routing/routing-1.0.xsd">
18861912
1887-
<route id="app_logout" path="/logout" methods="GET"/>
1913+
<route id="app_logout" path="/logout" methods="GET">
1914+
<path locale="en">/logout</path>
1915+
<path locale="fr">/deconnexion</path>
1916+
</route>
18881917
</routes>
18891918
18901919
.. code-block:: php
@@ -1893,14 +1922,14 @@ Next, you need to create a route for this URL (but not a controller):
18931922
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
18941923
18951924
return function (RoutingConfigurator $routes): void {
1896-
$routes->add('app_logout', '/logout')
1925+
$routes->add('app_logout', [
1926+
'en' => '/logout',
1927+
'fr' => '/deconnexion',
1928+
])
18971929
->methods(['GET'])
18981930
;
18991931
};
19001932
1901-
That's it! By sending a user to the ``app_logout`` route (i.e. to ``/logout``)
1902-
Symfony will un-authenticate the current user and redirect them.
1903-
19041933
Logout programmatically
19051934
~~~~~~~~~~~~~~~~~~~~~~~
19061935

0 commit comments

Comments
 (0)
0