@@ -1768,7 +1768,7 @@ To enable logging out, activate the ``logout`` config parameter under your fire
1768
1768
main :
1769
1769
# ...
1770
1770
logout :
1771
- path : app_logout
1771
+ path : /logout
1772
1772
1773
1773
# where to redirect after logout
1774
1774
# target: app_any_route
@@ -1789,11 +1789,10 @@ To enable logging out, activate the ``logout`` config parameter under your fire
1789
1789
<!-- ... -->
1790
1790
1791
1791
<firewall name =" main" >
1792
- <!-- ... -->
1793
- <logout path =" app_logout" />
1792
+ <logout path =" /logout" />
1794
1793
1795
1794
<!-- use "target" to configure where to redirect after logout
1796
- <logout path="app_logout " target="app_any_route"/>
1795
+ <logout path="/logout " target="app_any_route"/>
1797
1796
-->
1798
1797
</firewall >
1799
1798
</config >
@@ -1810,68 +1809,58 @@ To enable logging out, activate the ``logout`` config parameter under your fire
1810
1809
$mainFirewall = $security->firewall('main');
1811
1810
// ...
1812
1811
$mainFirewall->logout()
1813
- // the argument can be either a route name or a path
1814
- ->path('app_logout')
1812
+ ->path('/logout')
1815
1813
1816
1814
// where to redirect after logout
1817
1815
// ->target('app_any_route')
1818
1816
;
1819
1817
};
1820
1818
1821
- Next, you need to create a route for this URL (but not a controller):
1819
+ Symfony will then un-authenticate users navigating to the configured ``path ``,
1820
+ and redirect them to the configured ``target ``.
1822
1821
1823
- .. configuration-block ::
1824
-
1825
- .. code-block :: php-attributes
1822
+ .. tip ::
1826
1823
1827
- // src/Controller/SecurityController.php
1828
- namespace App\Controller;
1824
+ If you need to reference the logout path, you can use the `` _logout_<firewallname> ``
1825
+ route name (e.g. `` _logout_main ``).
1829
1826
1830
- use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
1831
- use Symfony\Component\Routing\Annotation\Route;
1827
+ If your project does not use :ref: ` Symfony Flex < symfony-flex >`, make sure
1828
+ you have imported the logout route loader in your routes:
1832
1829
1833
- class SecurityController extends AbstractController
1834
- {
1835
- #[Route('/logout', name: 'app_logout', methods: ['GET'])]
1836
- public function logout(): never
1837
- {
1838
- // controller can be blank: it will never be called!
1839
- throw new \Exception('Don\'t forget to activate logout in security.yaml');
1840
- }
1841
- }
1830
+ .. configuration-block ::
1842
1831
1843
1832
.. code-block :: yaml
1844
1833
1845
- # config/routes.yaml
1846
- app_logout :
1847
- path : / logout
1848
- methods : GET
1834
+ # config/routes/security .yaml
1835
+ _symfony_logout :
1836
+ resource : security.route_loader. logout
1837
+ type : service
1849
1838
1850
1839
.. code-block :: xml
1851
1840
1852
- <!-- config/routes.xml -->
1841
+ <!-- config/routes/security .xml -->
1853
1842
<?xml version =" 1.0" encoding =" UTF-8" ?>
1854
1843
<routes xmlns =" http://symfony.com/schema/routing"
1855
1844
xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
1856
1845
xsi : schemaLocation =" http://symfony.com/schema/routing
1857
1846
https://symfony.com/schema/routing/routing-1.0.xsd" >
1858
1847
1859
- <route id = " app_logout " path = " / logout" methods = " GET " />
1848
+ <import resource = " security.route_loader. logout" type = " service " />
1860
1849
</routes >
1861
1850
1862
1851
.. code-block :: php
1863
1852
1864
- // config/routes.php
1853
+ // config/routes/security .php
1865
1854
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
1866
1855
1867
- return function (RoutingConfigurator $routes): void {
1868
- $routes->add('app_logout', '/logout')
1869
- ->methods(['GET'])
1870
- ;
1856
+ return static function (RoutingConfigurator $routes): void {
1857
+ $routes->import('security.route_loader.logout', 'service');
1871
1858
};
1872
1859
1873
- That's it! By sending a user to the ``app_logout `` route (i.e. to ``/logout ``)
1874
- Symfony will un-authenticate the current user and redirect them.
1860
+ .. versionadded :: 6.4
1861
+
1862
+ The :class: `Symfony\\ Bundle\\ SecurityBundle\\ Routing\\ LogoutRouteLoader ` was
1863
+ introduced in Symfony 6.4.
1875
1864
1876
1865
Logout programmatically
1877
1866
~~~~~~~~~~~~~~~~~~~~~~~
@@ -1950,6 +1939,105 @@ to execute custom logic::
1950
1939
}
1951
1940
}
1952
1941
1942
+ Customizing Logout Path
1943
+ ~~~~~~~~~~~~~~~~~~~~~~~
1944
+
1945
+ Another option is to configure ``path `` as a route name. This can be useful
1946
+ if you want logout URIs to be dynamic (e.g. translated according to the
1947
+ current locale). In that case, you have to create this route yourself:
1948
+
1949
+ .. configuration-block ::
1950
+
1951
+ .. code-block :: yaml
1952
+
1953
+ # config/routes.yaml
1954
+ app_logout :
1955
+ path :
1956
+ en : /logout
1957
+ fr : /deconnexion
1958
+ methods : GET
1959
+
1960
+ .. code-block :: xml
1961
+
1962
+ <!-- config/routes.xml -->
1963
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
1964
+ <routes xmlns =" http://symfony.com/schema/routing"
1965
+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
1966
+ xsi : schemaLocation =" http://symfony.com/schema/routing
1967
+ https://symfony.com/schema/routing/routing-1.0.xsd" >
1968
+
1969
+ <route id =" app_logout" path =" /logout" methods =" GET" >
1970
+ <path locale =" en" >/logout</path >
1971
+ <path locale =" fr" >/deconnexion</path >
1972
+ </route >
1973
+ </routes >
1974
+
1975
+ .. code-block :: php
1976
+
1977
+ // config/routes.php
1978
+ use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
1979
+
1980
+ return function (RoutingConfigurator $routes): void {
1981
+ $routes->add('app_logout', [
1982
+ 'en' => '/logout',
1983
+ 'fr' => '/deconnexion',
1984
+ ])
1985
+ ->methods(['GET'])
1986
+ ;
1987
+ };
1988
+
1989
+ Then, pass the route name to the ``path `` option:
1990
+
1991
+ .. configuration-block ::
1992
+
1993
+ .. code-block :: yaml
1994
+
1995
+ # config/packages/security.yaml
1996
+ security :
1997
+ # ...
1998
+
1999
+ firewalls :
2000
+ main :
2001
+ # ...
2002
+ logout :
2003
+ path : app_logout
2004
+
2005
+ .. code-block :: xml
2006
+
2007
+ <!-- config/packages/security.xml -->
2008
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
2009
+ <srv : container xmlns =" http://symfony.com/schema/dic/security"
2010
+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
2011
+ xmlns : srv =" http://symfony.com/schema/dic/services"
2012
+ xsi : schemaLocation =" http://symfony.com/schema/dic/services
2013
+ https://symfony.com/schema/dic/services/services-1.0.xsd
2014
+ http://symfony.com/schema/dic/security
2015
+ https://symfony.com/schema/dic/security/security-1.0.xsd" >
2016
+
2017
+ <config >
2018
+ <!-- ... -->
2019
+
2020
+ <firewall name =" main" >
2021
+ <logout path =" app_logout" />
2022
+ </firewall >
2023
+ </config >
2024
+ </srv : container >
2025
+
2026
+ .. code-block :: php
2027
+
2028
+ // config/packages/security.php
2029
+ use Symfony\Config\SecurityConfig;
2030
+
2031
+ return static function (SecurityConfig $security): void {
2032
+ // ...
2033
+
2034
+ $mainFirewall = $security->firewall('main');
2035
+ // ...
2036
+ $mainFirewall->logout()
2037
+ ->path('app_logout')
2038
+ ;
2039
+ };
2040
+
1953
2041
.. _retrieving-the-user-object :
1954
2042
1955
2043
Fetching the User Object
0 commit comments