Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 6da43f6

Browse filesBrowse files
committed
Update “Managing Value Resolvers” section
1 parent 31edf49 commit 6da43f6
Copy full SHA for 6da43f6

File tree

1 file changed

+18
-38
lines changed
Filter options

1 file changed

+18
-38
lines changed

‎controller/value_resolver.rst

Copy file name to clipboardExpand all lines: controller/value_resolver.rst
+18-38Lines changed: 18 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -223,12 +223,13 @@ on their priority. For example, the ``SessionValueResolver`` will be called befo
223223
``SessionInterface $session = null`` to get the session if there is one, or ``null``
224224
if there is none.
225225

226-
But what if you *know* there will be a session? In that case every resolver running
227-
before ``SessionValueResolver`` is useless. Worse, some of these could actually
228-
provide a value before ``SessionValueResolver`` has a chance to (don't worry though,
229-
this won't happen with built-in resolvers). Since Symfony 6.3, this kind of issue
230-
can be resolved by leveraging the
231-
:class:`Symfony\\Component\\HttpKernel\\Attribute\\ValueResolver` attribute::
226+
In that specific case, you don't need any resolver running before
227+
``SessionValueResolver``, so skipping them would not only improve performance,
228+
but also prevent one of them providing a value before ``SessionValueResolver``
229+
has a chance to.
230+
231+
The :class:`Symfony\\Component\\HttpKernel\\Attribute\\ValueResolver` attribute lets you
232+
do this by "targeting" the resolver you want::
232233

233234
// src/Controller/SessionController.php
234235
namespace App\Controller;
@@ -244,7 +245,7 @@ can be resolved by leveraging the
244245
#[Route('/')]
245246
public function __invoke(
246247
#[ValueResolver(SessionValueResolver::class)]
247-
SessionInterface $session
248+
SessionInterface $session = null
248249
): Response
249250
{
250251
// ...
@@ -255,40 +256,19 @@ can be resolved by leveraging the
255256

256257
The ``ValueResolver`` attribute was introduced in Symfony 6.3.
257258

258-
You can target a resolver by passing its name (more on that later) as ``ValueResolver``'s
259-
first argument. For convenience, built-in resolvers' name are their FQCN.
260-
261-
By default, a targeted resolver is "pinned" to the argument holding the
262-
``ValueResolver`` attribute, meaning that only it will be called to provide a value,
263-
and that it will have to.
259+
In the example above, the ``SessionValueResolver`` will be called first because it is
260+
targeted. The ``DefaultValueResolver`` will be called next if no value has been provided;
261+
that's why we can assign ``$session`` a default value in the above example.
264262

265-
In the above example the ``DefaultValueResolver`` would never be called, so adding a
266-
default value to ``$session`` would be useless. If we need one, then it is fine not
267-
to use ``ValueResolver``.
268-
But then, what if we want to prevent an hypothetic ``EagerValueResolver`` to provide a
269-
value before ``SessionValueResolver``? Time to use ``ValueResolver``'s second argument!
270-
By passing it to ``true``, you can disable the targeted resolver::
271-
272-
// src/Controller/SessionController.php
273-
namespace App\Controller;
263+
We target a resolver by passing its name as ``ValueResolver``'s first argument.
264+
For convenience, built-in resolvers' name are their FQCN.
274265

275-
use App\ArgumentResolver\EagerValueResolver;
276-
use Symfony\Component\HttpFoundation\Response;
277-
use Symfony\Component\HttpFoundation\Session\SessionInterface;
278-
use Symfony\Component\HttpKernel\Attribute\ValueResolver;
279-
use Symfony\Component\Routing\Annotation\Route;
266+
The ``ValueResolver`` attribute can also be used to disable the targeted resolver
267+
by passing its ``$disabled`` argument to ``true``, in which case it won't be called.
268+
This is how :ref:`MapEntity allows to disable the EntityValueResolver
269+
for a specific controller <doctrine-entity-value-resolver>`.
270+
Yes, ``MapEntity`` extends ``ValueResolver``!
280271

281-
class SessionController
282-
{
283-
#[Route('/')]
284-
public function __invoke(
285-
#[ValueResolver(EagerValueResolver::class, disabled: true)]
286-
SessionInterface $session = null
287-
): Response
288-
{
289-
// ...
290-
}
291-
}
292272

293273
Adding a Custom Value Resolver
294274
------------------------------

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.