@@ -53,10 +53,9 @@ Symfony ships with five value resolvers in the HttpKernel component:
53
53
Adding a Custom Value Resolver
54
54
------------------------------
55
55
56
- Adding a new value resolver requires creating one class and one service
57
- definition. In the next example, you'll create a value resolver to inject the
58
- ``User `` object from the security system. Given you write the following
59
- controller::
56
+ In the next example, you'll create a value resolver to inject the object that
57
+ represents the current user whenever a controller method type-hints an argument
58
+ with the ``User `` class::
60
59
61
60
namespace AppBundle\Controller;
62
61
@@ -71,10 +70,30 @@ controller::
71
70
}
72
71
}
73
72
74
- Somehow you will have to get the ``User `` object and inject it into the controller.
75
- This can be done by implementing the
76
- :class: `Symfony\\ Component\\ HttpKernel\\ Controller\\ ArgumentValueResolverInterface `.
77
- This interface specifies that you have to implement two methods:
73
+ Beware that this feature is already provided by the `@ParamConverter `_
74
+ annotation from the SensioFrameworkExtraBundle. If you have that bundle
75
+ installed in your project, add this config to disable the auto-conversion of
76
+ type-hinted method arguments:
77
+
78
+ .. configuration-block ::
79
+
80
+ .. code-block :: yaml
81
+
82
+ # app/config/config.yml
83
+ sensio_framework_extra :
84
+ request :
85
+ converters : true
86
+ auto_convert : false
87
+
88
+ .. code-block :: xml
89
+
90
+ <sensio-framework-extra : config >
91
+ <request converters =" true" auto-convert =" true" />
92
+ </sensio-framework-extra : config >
93
+
94
+ Adding a new value resolver requires creating a class that implements
95
+ :class: `Symfony\\ Component\\ HttpKernel\\ Controller\\ ArgumentValueResolverInterface `
96
+ and defining a service for it. The interface defines two methods:
78
97
79
98
``supports() ``
80
99
This method is used to check whether the value resolver supports the
@@ -201,4 +220,5 @@ subrequests.
201
220
$user = null ``). The ``DefaultValueResolver `` is executed as the last
202
221
resolver and will use the default value if no value was already resolved.
203
222
223
+ .. _`@ParamConverter` : https://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/converters.html
204
224
.. _`yield` : http://php.net/manual/en/language.generators.syntax.php
0 commit comments