@@ -223,12 +223,13 @@ on their priority. For example, the ``SessionValueResolver`` will be called befo
223
223
``SessionInterface $session = null `` to get the session if there is one, or ``null ``
224
224
if there is none.
225
225
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::
232
233
233
234
// src/Controller/SessionController.php
234
235
namespace App\Controller;
@@ -244,7 +245,7 @@ can be resolved by leveraging the
244
245
#[Route('/')]
245
246
public function __invoke(
246
247
#[ValueResolver(SessionValueResolver::class)]
247
- SessionInterface $session
248
+ SessionInterface $session = null
248
249
): Response
249
250
{
250
251
// ...
@@ -255,40 +256,19 @@ can be resolved by leveraging the
255
256
256
257
The ``ValueResolver `` attribute was introduced in Symfony 6.3.
257
258
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.
264
262
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.
274
265
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 ``!
280
271
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
- }
292
272
293
273
Adding a Custom Value Resolver
294
274
------------------------------
0 commit comments