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 f06d106

Browse filesBrowse files
author
Mathieu
committed
Add #[ValueResolver] for specifying a controller argument resolver
1 parent 5a2af0f commit f06d106
Copy full SHA for f06d106

File tree

1 file changed

+44
-0
lines changed
Filter options

1 file changed

+44
-0
lines changed

‎controller/value_resolver.rst

Copy file name to clipboardExpand all lines: controller/value_resolver.rst
+44Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.