Skip to content

Commit f2bfba1

Browse files
Explaining default logout path
The existing code sample `path: app_logout` is wrong, since you don't have to pass the route's *name*, but rather its `path` (i.e. `/logout`)! Please double-check the XML and PHP config - I merely guessed those. Thanks to symfony#13424 (review)
1 parent d4bb765 commit f2bfba1

File tree

1 file changed

+62
-11
lines changed

1 file changed

+62
-11
lines changed

security.rst

Lines changed: 62 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -768,6 +768,60 @@ Logging Out
768768

769769
To enable logging out, activate the ``logout`` config parameter under your firewall:
770770

771+
.. configuration-block::
772+
773+
.. code-block:: yaml
774+
775+
# config/packages/security.yaml
776+
security:
777+
# ...
778+
779+
firewalls:
780+
main:
781+
# ...
782+
logout: ~
783+
784+
.. code-block:: xml
785+
786+
<!-- config/packages/security.xml -->
787+
<?xml version="1.0" encoding="UTF-8"?>
788+
<srv:container xmlns="http://symfony.com/schema/dic/security"
789+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
790+
xmlns:srv="http://symfony.com/schema/dic/services"
791+
xsi:schemaLocation="http://symfony.com/schema/dic/services
792+
https://symfony.com/schema/dic/services/services-1.0.xsd">
793+
794+
<config>
795+
<!-- ... -->
796+
797+
<firewall name="secured_area">
798+
<!-- ... -->
799+
<logout />
800+
</firewall>
801+
</config>
802+
</srv:container>
803+
804+
.. code-block:: php
805+
806+
// config/packages/security.php
807+
$container->loadFromExtension('security', [
808+
// ...
809+
810+
'firewalls' => [
811+
'secured_area' => [
812+
// ...
813+
'logout' => [],
814+
],
815+
],
816+
]);
817+
818+
819+
And that's it! By sending a user to ``/logout``, Symfony will un-authenticate
820+
the current user.
821+
822+
If you want to change the path from the default ``/logout`` to a custom url,
823+
you need to set the `path` option *and* setup a matching route like this:
824+
771825
.. configuration-block::
772826

773827
.. code-block:: yaml
@@ -780,7 +834,7 @@ To enable logging out, activate the ``logout`` config parameter under your fire
780834
main:
781835
# ...
782836
logout:
783-
path: app_logout
837+
path: /my-logout
784838
785839
# where to redirect after logout
786840
# target: app_any_route
@@ -800,7 +854,7 @@ To enable logging out, activate the ``logout`` config parameter under your fire
800854
801855
<firewall name="secured_area">
802856
<!-- ... -->
803-
<logout path="app_logout"/>
857+
<logout path="/my-logout"/>
804858
</firewall>
805859
</config>
806860
</srv:container>
@@ -814,12 +868,12 @@ To enable logging out, activate the ``logout`` config parameter under your fire
814868
'firewalls' => [
815869
'secured_area' => [
816870
// ...
817-
'logout' => ['path' => 'app_logout'],
871+
'logout' => ['path' => '/my-logout'],
818872
],
819873
],
820874
]);
821875
822-
Next, you'll need to create a route for this URL (but not a controller):
876+
Now you need to create a route for this URL (but not a controller):
823877

824878
.. configuration-block::
825879

@@ -834,7 +888,7 @@ Next, you'll need to create a route for this URL (but not a controller):
834888
class SecurityController extends AbstractController
835889
{
836890
/**
837-
* @Route("/logout", name="app_logout", methods={"GET"})
891+
* @Route("/my-logout", name="app_logout", methods={"GET"})
838892
*/
839893
public function logout()
840894
{
@@ -847,7 +901,7 @@ Next, you'll need to create a route for this URL (but not a controller):
847901
848902
# config/routes.yaml
849903
app_logout:
850-
path: /logout
904+
path: /my-logout
851905
methods: GET
852906
853907
.. code-block:: xml
@@ -859,7 +913,7 @@ Next, you'll need to create a route for this URL (but not a controller):
859913
xsi:schemaLocation="http://symfony.com/schema/routing
860914
https://symfony.com/schema/routing/routing-1.0.xsd">
861915
862-
<route id="app_logout" path="/logout" methods="GET"/>
916+
<route id="app_logout" path="/my-logout" methods="GET"/>
863917
</routes>
864918
865919
.. code-block:: php
@@ -868,14 +922,11 @@ Next, you'll need to create a route for this URL (but not a controller):
868922
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
869923
870924
return function (RoutingConfigurator $routes) {
871-
$routes->add('logout', '/logout')
925+
$routes->add('app_logout', '/my-logout')
872926
->methods(['GET'])
873927
;
874928
};
875929
876-
And that's it! By sending a user to the ``app_logout`` route (i.e. to ``/logout``)
877-
Symfony will un-authenticate the current user and redirect them.
878-
879930
.. tip::
880931

881932
Need more control of what happens after logout? Add a ``success_handler`` key

0 commit comments

Comments
 (0)