Skip to content

Commit 8f2c087

Browse files
javiereguiluzGuikingone
authored andcommitted
Update the argument value resolver article
1 parent a99a0a7 commit 8f2c087

File tree

1 file changed

+28
-8
lines changed

1 file changed

+28
-8
lines changed

controller/argument_value_resolver.rst

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,9 @@ Symfony ships with five value resolvers in the HttpKernel component:
4545
Adding a Custom Value Resolver
4646
------------------------------
4747

48-
Adding a new value resolver requires creating one class and one service
49-
definition. In the next example, you'll create a value resolver to inject the
50-
``User`` object from the security system. Given you write the following
51-
controller::
48+
In the next example, you'll create a value resolver to inject the object that
49+
represents the current user whenever a controller method type-hints an argument
50+
with the ``User`` class::
5251

5352
namespace App\Controller;
5453

@@ -63,10 +62,30 @@ controller::
6362
}
6463
}
6564

66-
Somehow you will have to get the ``User`` object and inject it into the controller.
67-
This can be done by implementing the
68-
:class:`Symfony\\Component\\HttpKernel\\Controller\\ArgumentValueResolverInterface`.
69-
This interface specifies that you have to implement two methods:
65+
Beware that this feature is already provided by the `@ParamConverter`_
66+
annotation from the SensioFrameworkExtraBundle. If you have that bundle
67+
installed in your project, add this config to disable the auto-conversion of
68+
type-hinted method arguments:
69+
70+
.. configuration-block::
71+
72+
.. code-block:: yaml
73+
74+
# app/config/config.yml
75+
sensio_framework_extra:
76+
request:
77+
converters: true
78+
auto_convert: false
79+
80+
.. code-block:: xml
81+
82+
<sensio-framework-extra:config>
83+
<request converters="true" auto-convert="true" />
84+
</sensio-framework-extra:config>
85+
86+
Adding a new value resolver requires creating a class that implements
87+
:class:`Symfony\\Component\\HttpKernel\\Controller\\ArgumentValueResolverInterface`
88+
and defining a service for it. The interface defines two methods:
7089

7190
``supports()``
7291
This method is used to check whether the value resolver supports the
@@ -193,4 +212,5 @@ subrequests.
193212
$user = null``). The ``DefaultValueResolver`` is executed as the last
194213
resolver and will use the default value if no value was already resolved.
195214

215+
.. _`@ParamConverter`: https://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/converters.html
196216
.. _`yield`: http://php.net/manual/en/language.generators.syntax.php

0 commit comments

Comments
 (0)