@@ -1801,7 +1801,7 @@ To enable logging out, activate the ``logout`` config parameter under your fire
1801
1801
main :
1802
1802
# ...
1803
1803
logout :
1804
- path : app_logout
1804
+ path : /logout
1805
1805
1806
1806
# where to redirect after logout
1807
1807
# target: app_any_route
@@ -1822,11 +1822,10 @@ To enable logging out, activate the ``logout`` config parameter under your fire
1822
1822
<!-- ... -->
1823
1823
1824
1824
<firewall name =" main" >
1825
- <!-- ... -->
1826
- <logout path =" app_logout" />
1825
+ <logout path =" /logout" />
1827
1826
1828
1827
<!-- use "target" to configure where to redirect after logout
1829
- <logout path="app_logout " target="app_any_route"/>
1828
+ <logout path="/logout " target="app_any_route"/>
1830
1829
-->
1831
1830
</firewall >
1832
1831
</config >
@@ -1843,68 +1842,58 @@ To enable logging out, activate the ``logout`` config parameter under your fire
1843
1842
$mainFirewall = $security->firewall('main');
1844
1843
// ...
1845
1844
$mainFirewall->logout()
1846
- // the argument can be either a route name or a path
1847
- ->path('app_logout')
1845
+ ->path('/logout')
1848
1846
1849
1847
// where to redirect after logout
1850
1848
// ->target('app_any_route')
1851
1849
;
1852
1850
};
1853
1851
1854
- Next, you need to create a route for this URL (but not a controller):
1852
+ Symfony will then un-authenticate users navigating to the configured ``path ``,
1853
+ and redirect them to the configured ``target ``.
1855
1854
1856
- .. configuration-block ::
1857
-
1858
- .. code-block :: php-attributes
1855
+ .. tip ::
1859
1856
1860
- // src/Controller/SecurityController.php
1861
- namespace App\Controller;
1857
+ If you need to reference the logout path, you can use the `` _logout_<firewallname> ``
1858
+ route name (e.g. `` _logout_main ``).
1862
1859
1863
- use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
1864
- use Symfony\Component\Routing\Annotation\Route;
1860
+ If your project does not use :ref: ` Symfony Flex < symfony-flex >`, make sure
1861
+ you have imported the logout route loader in your routes:
1865
1862
1866
- class SecurityController extends AbstractController
1867
- {
1868
- #[Route('/logout', name: 'app_logout', methods: ['GET'])]
1869
- public function logout(): never
1870
- {
1871
- // controller can be blank: it will never be called!
1872
- throw new \Exception('Don\'t forget to activate logout in security.yaml');
1873
- }
1874
- }
1863
+ .. configuration-block ::
1875
1864
1876
1865
.. code-block :: yaml
1877
1866
1878
- # config/routes.yaml
1879
- app_logout :
1880
- path : / logout
1881
- methods : GET
1867
+ # config/routes/security .yaml
1868
+ _symfony_logout :
1869
+ resource : security.route_loader. logout
1870
+ type : service
1882
1871
1883
1872
.. code-block :: xml
1884
1873
1885
- <!-- config/routes.xml -->
1874
+ <!-- config/routes/security .xml -->
1886
1875
<?xml version =" 1.0" encoding =" UTF-8" ?>
1887
1876
<routes xmlns =" http://symfony.com/schema/routing"
1888
1877
xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
1889
1878
xsi : schemaLocation =" http://symfony.com/schema/routing
1890
1879
https://symfony.com/schema/routing/routing-1.0.xsd" >
1891
1880
1892
- <route id = " app_logout " path = " / logout" methods = " GET " />
1881
+ <import resource = " security.route_loader. logout" type = " service " />
1893
1882
</routes >
1894
1883
1895
1884
.. code-block :: php
1896
1885
1897
- // config/routes.php
1886
+ // config/routes/security .php
1898
1887
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
1899
1888
1900
- return function (RoutingConfigurator $routes): void {
1901
- $routes->add('app_logout', '/logout')
1902
- ->methods(['GET'])
1903
- ;
1889
+ return static function (RoutingConfigurator $routes): void {
1890
+ $routes->import('security.route_loader.logout', 'service');
1904
1891
};
1905
1892
1906
- That's it! By sending a user to the ``app_logout `` route (i.e. to ``/logout ``)
1907
- Symfony will un-authenticate the current user and redirect them.
1893
+ .. versionadded :: 6.4
1894
+
1895
+ The :class: `Symfony\\ Bundle\\ SecurityBundle\\ Routing\\ LogoutRouteLoader ` was
1896
+ introduced in Symfony 6.4.
1908
1897
1909
1898
Logout programmatically
1910
1899
~~~~~~~~~~~~~~~~~~~~~~~
@@ -1994,6 +1983,105 @@ to execute custom logic::
1994
1983
}
1995
1984
}
1996
1985
1986
+ Customizing Logout Path
1987
+ ~~~~~~~~~~~~~~~~~~~~~~~
1988
+
1989
+ Another option is to configure ``path `` as a route name. This can be useful
1990
+ if you want logout URIs to be dynamic (e.g. translated according to the
1991
+ current locale). In that case, you have to create this route yourself:
1992
+
1993
+ .. configuration-block ::
1994
+
1995
+ .. code-block :: yaml
1996
+
1997
+ # config/routes.yaml
1998
+ app_logout :
1999
+ path :
2000
+ en : /logout
2001
+ fr : /deconnexion
2002
+ methods : GET
2003
+
2004
+ .. code-block :: xml
2005
+
2006
+ <!-- config/routes.xml -->
2007
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
2008
+ <routes xmlns =" http://symfony.com/schema/routing"
2009
+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
2010
+ xsi : schemaLocation =" http://symfony.com/schema/routing
2011
+ https://symfony.com/schema/routing/routing-1.0.xsd" >
2012
+
2013
+ <route id =" app_logout" path =" /logout" methods =" GET" >
2014
+ <path locale =" en" >/logout</path >
2015
+ <path locale =" fr" >/deconnexion</path >
2016
+ </route >
2017
+ </routes >
2018
+
2019
+ .. code-block :: php
2020
+
2021
+ // config/routes.php
2022
+ use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
2023
+
2024
+ return function (RoutingConfigurator $routes): void {
2025
+ $routes->add('app_logout', [
2026
+ 'en' => '/logout',
2027
+ 'fr' => '/deconnexion',
2028
+ ])
2029
+ ->methods(['GET'])
2030
+ ;
2031
+ };
2032
+
2033
+ Then, pass the route name to the ``path `` option:
2034
+
2035
+ .. configuration-block ::
2036
+
2037
+ .. code-block :: yaml
2038
+
2039
+ # config/packages/security.yaml
2040
+ security :
2041
+ # ...
2042
+
2043
+ firewalls :
2044
+ main :
2045
+ # ...
2046
+ logout :
2047
+ path : app_logout
2048
+
2049
+ .. code-block :: xml
2050
+
2051
+ <!-- config/packages/security.xml -->
2052
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
2053
+ <srv : container xmlns =" http://symfony.com/schema/dic/security"
2054
+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
2055
+ xmlns : srv =" http://symfony.com/schema/dic/services"
2056
+ xsi : schemaLocation =" http://symfony.com/schema/dic/services
2057
+ https://symfony.com/schema/dic/services/services-1.0.xsd
2058
+ http://symfony.com/schema/dic/security
2059
+ https://symfony.com/schema/dic/security/security-1.0.xsd" >
2060
+
2061
+ <config >
2062
+ <!-- ... -->
2063
+
2064
+ <firewall name =" main" >
2065
+ <logout path =" app_logout" />
2066
+ </firewall >
2067
+ </config >
2068
+ </srv : container >
2069
+
2070
+ .. code-block :: php
2071
+
2072
+ // config/packages/security.php
2073
+ use Symfony\Config\SecurityConfig;
2074
+
2075
+ return static function (SecurityConfig $security): void {
2076
+ // ...
2077
+
2078
+ $mainFirewall = $security->firewall('main');
2079
+ // ...
2080
+ $mainFirewall->logout()
2081
+ ->path('app_logout')
2082
+ ;
2083
+ };
2084
+
1997
2085
.. _retrieving-the-user-object :
1998
2086
1999
2087
Fetching the User Object
0 commit comments