10000 Added some extra info about the DefaultValueResolver · linaori/symfony-docs@2e41c07 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2e41c07

Browse files
author
Iltar van der Berg
committed
Added some extra info about the DefaultValueResolver
1 parent a07fcc3 commit 2e41c07

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

cookbook/controller/argument_value_resolver.rst

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ object by adding a ``Request`` argument to your controller. This is done via the
1212
:class:`Symfony\\Component\\HttpKernel\\Controller\\ArgumentResolver`. By creating and registering custom
1313
argument value resolvers, you can extend this functionality.
1414

15-
1615
Functionality Shipped With The HttpKernel
1716
-----------------------------------------
1817

@@ -34,14 +33,13 @@ Symfony ships with four value resolvers in the HttpKernel:
3433
.. note::
3534

3635
Prior to Symfony 3.1, this logic was resolved within the ``ControllerResolver``. The old
37-
functionality is moved to the ``LegacyArgumentResolver``, which contains the previously
38-
used resolving logic.
36+
functionality is rewritten to the aforementioned value resolvers.
3937

4038
Adding a Custom Value Resolver
4139
------------------------------
4240

4341
Adding a new value resolver requires one class and one service defintion. In the next example,
44-
you'll create a value resolver to inject the ``User`` object from the security system.. Given
42+
you'll create a value resolver to inject the ``User`` object from the security system. Given
4543
you write the following action::
4644

4745
namespace AppBundle\Controller;
@@ -76,7 +74,7 @@ This interface specifies that you have to implement two methods::
7674

7775
Both methods get the ``Request`` object, which is the current request, and an
7876
:class:`Symfony\\Component\\HttpKernel\\ControllerMetadata\\ArgumentMetadata`.
79-
This object contains all informations retrieved from the method signature for the
77+
This object contains all information retrieved from the method signature for the
8078
current argument.
8179

8280
.. note::
@@ -121,6 +119,23 @@ the current security token. This token can be retrieved from the token storage.:
121119
}
122120
}
123121

122+
In order to get the actual ``User`` object in your argument, the given value should fulfill the
123+
following requirements:
124+
125+
* The argument type (of the method signature) must be typehinted as ``User``;
126+
* The security token must be present;
127+
* The value should be an instance of the ``User``.
128+
129+
When all those requirements are met and true is returned, the ``ArgumentResolver`` calls
130+
``resolve()`` with the same values as it called ``supports()``.
131+
132+
.. tip::
133+
134+
You can leverage the ``DefaultValueResolver`` by making your resolver accept only mandatory
135+
arguments. Given your signature is `User $user = null`, the above example will not hit ``resolve()``
136+
as one of the conditions does not match. Eventually when the ``DefaultValueResolver`` is asked to
137+
resolve this, it will simply add the default value from the method signature, which results in ``null``.
138+
124139
That's it! Now all you have to do is add the configuration for the service container. This
125140
can be done by tagging the service with ``kernel.argument_resolver`` and adding a priority.
126141

0 commit comments

Comments
 (0)
0