Skip to content

Commit 90ce7e6

Browse files
committed
feature #17328 docs: add docs for programmatic logout (johnkrovitch)
This PR was merged into the 6.2 branch. Discussion ---------- docs: add docs for programmatic logout This PR adds documentation for the new feature "programmatic logout" merge in the PR symfony/symfony#41406 Commits ------- ac46df4 [#17328] Minor changes 5611f88 docs: add docs for programmatic logout
2 parents 76eeecb + ac46df4 commit 90ce7e6

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

security.rst

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1770,6 +1770,45 @@ Next, you need to create a route for this URL (but not a controller):
17701770
That's it! By sending a user to the ``app_logout`` route (i.e. to ``/logout``)
17711771
Symfony will un-authenticate the current user and redirect them.
17721772

1773+
Logout programmatically
1774+
~~~~~~~~~~~~~~~~~~~~~~~
1775+
1776+
.. versionadded:: 6.2
1777+
1778+
The :class:`Symfony\Bundle\SecurityBundle\Security\Security <Symfony\\Bundle\\SecurityBundle\\Security\\Security>`
1779+
class was introduced in Symfony 6.2. Prior to 6.2, it was called
1780+
``Symfony\Component\Security\Core\Security``.
1781+
1782+
.. versionadded:: 6.2
1783+
1784+
The :method:`Symfony\\Bundle\\SecurityBundle\\Security\\Security::logout`
1785+
method was introduced in Symfony 6.2.
1786+
1787+
You can logout user programmatically using the ``logout()`` method of the
1788+
:class:`Symfony\\Bundle\\SecurityBundle\\Security\\Security` helper::
1789+
1790+
// src/Controller/SecurityController.php
1791+
namespace App\Controller\SecurityController;
1792+
1793+
use Symfony\Bundle\SecurityBundle\Security\Security;
1794+
1795+
class SecurityController
1796+
{
1797+
public function someAction(Security $security): Response
1798+
{
1799+
// logout the user in on the current firewall
1800+
$response = $security->logout();
1801+
1802+
// you can also disable the csrf logout
1803+
$response = $security->logout(false);
1804+
1805+
// ... return $response (if set) or e.g. redirect to the homepage
1806+
}
1807+
}
1808+
1809+
The user will be logout from the firewall of the request. If the request is
1810+
not behind a firewall a ``\LogicException`` will be thrown.
1811+
17731812
Customizing Logout
17741813
~~~~~~~~~~~~~~~~~~
17751814

0 commit comments

Comments
 (0)