Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e99a654

Browse files
author
Mathieu
committed
Add #[ValueResolver] for specifying a controller argument resolver
1 parent d07e93b commit e99a654

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

controller/value_resolver.rst

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,3 +355,47 @@ command to see which argument resolvers are present and in which order they run:
355355
.. code-block:: terminal
356356
357357
$ php bin/console debug:container debug.argument_resolver.inner --show-arguments
358+
359+
Using a Specific Value Resolver
360+
-------------------------------
361+
362+
.. versionadded:: 6.3
363+
364+
The ``ValueResolver`` attribute was introduced in Symfony 6.3.
365+
366+
The priority-based system is designed to support the vast majority of use-cases,
367+
but it has limits: every resolver will be called until one provide some value.
368+
Not only is this inefficient if you already know how your parameter must be resolved,
369+
you could not even be sure which resolver did provide a value.
370+
371+
You can tackle these two issues at once by adding a ``ValueResolver`` attribute
372+
to such parameters:
373+
374+
// src/Controller/DefaultController.php
375+
namespace App\Controller;
376+
377+
use Symfony\Component\HttpFoundation\Response;
378+
use Symfony\Component\HttpKernel\Attribute\ValueResolver;
379+
use Symfony\Component\HttpKernel\Controller\ArgumentResolver\DefaultValueResolver;
380+
use Symfony\Component\Routing\Annotation\Route;
381+
382+
class DefaultController
383+
{
384+
#[Route('/{page}')]
385+
public function list(
386+
#[ValueResolver(DefaultValueResolver::class)]
387+
int $page = 1,
388+
): Response {
389+
/*
390+
* Here $page would always be 1: the RequestAttributeValueResolver would not kick in
391+
* because we asked the DefaultValueResolver only to provide a value.
392+
*/
393+
}
394+
}
395+
396+
Its only argument is the resolver's name. That is,
397+
its definition's ``controller.argument_value_resolver`` tag ``name`` attribute.
398+
For built-in resolvers, this is the same as their FQCN.
399+
400+
Note that the specified resolver **must** provide a value.
401+
Else, an exception will be thrown.

0 commit comments

Comments
 (0)