@@ -1796,7 +1796,7 @@ To enable logging out, activate the ``logout`` config parameter under your fire
1796
1796
main :
1797
1797
# ...
1798
1798
logout :
1799
- path : app_logout
1799
+ path : /logout
1800
1800
1801
1801
# where to redirect after logout
1802
1802
# target: app_any_route
@@ -1817,8 +1817,7 @@ To enable logging out, activate the ``logout`` config parameter under your fire
1817
1817
<!-- ... -->
1818
1818
1819
1819
<firewall name =" main" >
1820
- <!-- ... -->
1821
- <logout path =" app_logout" />
1820
+ <logout path =" /logout" />
1822
1821
1823
1822
<!-- use "target" to configure where to redirect after logout
1824
1823
<logout path="app_logout" target="app_any_route"/>
@@ -1838,41 +1837,68 @@ To enable logging out, activate the ``logout`` config parameter under your fire
1838
1837
$mainFirewall = $security->firewall('main');
1839
1838
// ...
1840
1839
$mainFirewall->logout()
1841
- // the argument can be either a route name or a path
1842
- ->path('app_logout')
1840
+ ->path('/logout')
1843
1841
1844
1842
// where to redirect after logout
1845
1843
// ->target('app_any_route')
1846
1844
;
1847
1845
};
1848
1846
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:
1850
1853
1851
1854
.. configuration-block ::
1852
1855
1853
- .. code-block :: php-attributes
1856
+ .. code-block :: yaml
1854
1857
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
1857
1862
1858
- use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
1859
- use Symfony\Component\Routing\Annotation\Route;
1863
+ .. code-block :: xml
1860
1864
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 ::
1870
1894
1871
1895
.. code-block :: yaml
1872
1896
1873
1897
# config/routes.yaml
1874
1898
app_logout :
1875
- path : /logout
1899
+ path :
1900
+ en : /logout
1901
+ fr : /deconnexion
1876
1902
methods : GET
1877
1903
1878
1904
.. code-block :: xml
@@ -1884,7 +1910,10 @@ Next, you need to create a route for this URL (but not a controller):
1884
1910
xsi : schemaLocation =" http://symfony.com/schema/routing
1885
1911
https://symfony.com/schema/routing/routing-1.0.xsd" >
1886
1912
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 >
1888
1917
</routes >
1889
1918
1890
1919
.. code-block :: php
@@ -1893,14 +1922,14 @@ Next, you need to create a route for this URL (but not a controller):
1893
1922
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
1894
1923
1895
1924
return function (RoutingConfigurator $routes): void {
1896
- $routes->add('app_logout', '/logout')
1925
+ $routes->add('app_logout', [
1926
+ 'en' => '/logout',
1927
+ 'fr' => '/deconnexion',
1928
+ ])
1897
1929
->methods(['GET'])
1898
1930
;
1899
1931
};
1900
1932
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
-
1904
1933
Logout programmatically
1905
1934
~~~~~~~~~~~~~~~~~~~~~~~
1906
1935
0 commit comments