@@ -12,7 +12,6 @@ object by adding a ``Request`` argument to your controller. This is done via the
12
12
:class: `Symfony\\ Component\\ HttpKernel\\ Controller\\ ArgumentResolver `. By creating and registering custom
13
13
argument value resolvers, you can extend this functionality.
14
14
15
-
16
15
Functionality Shipped With The HttpKernel
17
16
-----------------------------------------
18
17
@@ -34,14 +33,13 @@ Symfony ships with four value resolvers in the HttpKernel:
34
33
.. note ::
35
34
36
35
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.
39
37
40
38
Adding a Custom Value Resolver
41
39
------------------------------
42
40
43
41
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
45
43
you write the following action::
46
44
47
45
namespace AppBundle\Controller;
@@ -76,7 +74,7 @@ This interface specifies that you have to implement two methods::
76
74
77
75
Both methods get the ``Request `` object, which is the current request, and an
78
76
: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
80
78
current argument.
81
79
82
80
.. note ::
@@ -121,6 +119,23 @@ the current security token. This token can be retrieved from the token storage.:
121
119
}
122
120
}
123
121
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
+
124
139
That's it! Now all you have to do is add the configuration for the service container. This
125
140
can be done by tagging the service with ``kernel.argument_resolver `` and adding a priority.
126
141
0 commit comments