Skip to content

Commit f8d5eae

Browse files
committed
minor #17000 [Security] Document the getFirewallConfig() method (javiereguiluz)
This PR was squashed before being merged into the 6.2 branch. Discussion ---------- [Security] Document the getFirewallConfig() method Fixes #16855. Commits ------- 3abdbc3 [Security] Document the getFirewallConfig() method
2 parents e446184 + 3abdbc3 commit f8d5eae

File tree

1 file changed

+36
-2
lines changed

1 file changed

+36
-2
lines changed

security.rst

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -605,8 +605,42 @@ anything else within your firewall in the :ref:`access control
605605
606606
$ composer require --dev symfony/profiler-pack
607607
608-
Now that we understand our firewall, the next step is to create a way for your
609-
users to authenticate!
608+
Fetching the Firewall Configuration for a Request
609+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
610+
611+
If you need to get the configuration of the firewall that matched a given request,
612+
use the :class:`Symfony\\Bundle\\SecurityBundle\\Security\\Security` service::
613+
614+
// src/Service/ExampleService.php
615+
// ...
616+
617+
use Symfony\Bundle\SecurityBundle\Security\Security;
618+
use Symfony\Component\HttpFoundation\RequestStack;
619+
620+
class ExampleService
621+
{
622+
private Security $security;
623+
624+
public function __construct(Security $security, RequestStack $requestStack)
625+
{
626+
$this->requestStack = $requestStack;
627+
// Avoid calling getFirewallConfig() in the constructor: auth may not
628+
// be complete yet. Instead, store the entire Security object.
629+
$this->security = $security;
630+
}
631+
632+
public function someMethod()
633+
{
634+
$request = $this->requestStack->getCurrentRequest();
635+
$firewallName = $this->security->getFirewallConfig($request)?->getName();
636+
637+
// ...
638+
}
639+
}
640+
641+
.. versionadded:: 6.2
642+
643+
The ``getFirewallConfig()`` method was introduced in Symfony 6.2.
610644

611645
.. _security-authenticators:
612646

0 commit comments

Comments
 (0)