@@ -45,10 +45,9 @@ Symfony ships with five value resolvers in the HttpKernel component:
45
45
Adding a Custom Value Resolver
46
46
------------------------------
47
47
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::
52
51
53
52
namespace App\Controller;
54
53
@@ -63,10 +62,30 @@ controller::
63
62
}
64
63
}
65
64
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:
70
89
71
90
``supports() ``
72
91
This method is used to check whether the value resolver supports the
@@ -193,4 +212,5 @@ subrequests.
193
212
$user = null ``). The ``DefaultValueResolver `` is executed as the last
194
213
resolver and will use the default value if no value was already resolved.
195
214
215
+ .. _`@ParamConverter` : https://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/converters.html
196
216
.. _`yield` : http://php.net/manual/en/language.generators.syntax.php
0 commit comments