@@ -8,9 +8,10 @@ Extending Action Argument Resolving
8
8
The ``ArgumentResolver `` and value resolvers are added in Symfony 3.1.
9
9
10
10
In the book, you've learned that you can get the :class: `Symfony\\ Component\\ HttpFoundation\\ Request `
11
- object by adding a ``Request `` argument to your controller. This is done via the
12
- :class: `Symfony\\ Component\\ HttpKernel\\ Controller\\ ArgumentResolver `. By creating and registering custom
13
- argument value resolvers, you can extend this functionality.
11
+ object by adding a ``Request `` argument to your controller. This is done
12
+ via the :class: `Symfony\\ Component\\ HttpKernel\\ Controller\\ ArgumentResolver `.
13
+ By creating and registering custom argument value resolvers, you can extend
14
+ this functionality.
14
15
15
16
Functionality Shipped With The HttpKernel
16
17
-----------------------------------------
@@ -21,26 +22,29 @@ Symfony ships with four value resolvers in the HttpKernel:
21
22
Attempts to find a request attribute that matches the name of the argument.
22
23
23
24
:class: `Symfony\\ Component\\ HttpKernel\\ Controller\\ ArgumentValueResolver\\ RequestValueResolver `
24
- Injects the current ``Request `` if type-hinted with ``Request ``, or a sub-class thereof.
25
+ Injects the current ``Request `` if type-hinted with ``Request ``, or a
26
+ sub-class thereof.
25
27
26
28
:class: `Symfony\\ Component\\ HttpKernel\\ Controller\\ ArgumentValueResolver\\ DefaultValueResolver `
27
- Will set the default value of the argument if present and the argument is optional.
29
+ Will set the default value of the argument if present and the argument
30
+ is optional.
28
31
29
32
:class: `Symfony\\ Component\\ HttpKernel\\ Controller\\ ArgumentValueResolver\\ VariadicValueResolver `
30
- Verifies in the request if your data is an array and will add all of them to the argument list.
31
- When the action is called, the last (variadic) argument will contain all the values of this array.
33
+ Verifies in the request if your data is an array and will add all of
34
+ them to the argument list. When the action is called, the last (variadic)
35
+ argument will contain all the values of this array.
32
36
33
37
.. note ::
34
38
35
- Prior to Symfony 3.1, this logic was resolved within the ``ControllerResolver ``. The old
36
- functionality is rewritten to the aforementioned value resolvers.
39
+ Prior to Symfony 3.1, this logic was resolved within the ``ControllerResolver ``.
40
+ The old functionality is rewritten to the aforementioned value resolvers.
37
41
38
42
Adding a Custom Value Resolver
39
43
------------------------------
40
44
41
- Adding a new value resolver requires one class and one service defintion. In the next example,
42
- you'll create a value resolver to inject the ``User `` object from the security system. Given
43
- you write the following action::
45
+ Adding a new value resolver requires one class and one service defintion.
46
+ In the next example, you'll create a value resolver to inject the ``User ``
47
+ object from the security system. Given you write the following action::
44
48
45
49
namespace AppBundle\Controller;
46
50
@@ -55,8 +59,8 @@ you write the following action::
55
59
}
56
60
}
57
61
58
- Somehow you will have to get the ``User `` object and inject it into the controller. This can be done
59
- by implementing the :class: `Symfony\\ Component\\ HttpKernel\\ Controller\\ ArgumentValueResolverInterface `.
62
+ Somehow you will have to get the ``User `` object and inject it into the controller.
63
+ This can be done by implementing the :class: `Symfony\\ Component\\ HttpKernel\\ Controller\\ ArgumentValueResolverInterface `.
60
64
This interface specifies that you have to implement two methods::
61
65
62
66
interface ArgumentValueResolverInterface
@@ -74,19 +78,21 @@ This interface specifies that you have to implement two methods::
74
78
75
79
Both methods get the ``Request `` object, which is the current request, and an
76
80
:class: `Symfony\\ Component\\ HttpKernel\\ ControllerMetadata\\ ArgumentMetadata `.
77
- This object contains all information retrieved from the method signature for the
78
- current argument.
81
+ This object contains all information retrieved from the method signature for
82
+ the current argument.
79
83
80
84
.. note ::
81
85
82
86
The ``ArgumentMetadata `` is a simple data container created by the
83
- :class: `Symfony\\ Component\\ HttpKernel\\ ControllerMetadata\\ ArgumentMetadataFactory `. This
84
- factory will work on every supported PHP version but might give different results. E.g. the
85
- ``isVariadic() `` will never return true on PHP 5.5 and only on PHP 7.0 and higher it will give
86
- you basic types when calling ``getType() ``.
87
+ :class: `Symfony\\ Component\\ HttpKernel\\ ControllerMetadata\\ ArgumentMetadataFactory `.
88
+ This factory will work on every supported PHP version but might give
89
+ different results. E.g. the ``isVariadic() `` will never return true on
90
+ PHP 5.5 and only on PHP 7.0 and higher it will give you basic types when
91
+ calling ``getType() ``.
87
92
88
- Now that you know what to do, you can implement this interface. To get the current ``User ``, you need
89
- the current security token. This token can be retrieved from the token storage.::
93
+ Now that you know what to do, you can implement this interface. To get the
94
+ current ``User ``, you need the current security token. This token can be
95
+ retrieved from the token storage::
90
96
91
97
namespace AppBundle\ArgumentValueResolver;
92
98
@@ -124,33 +130,37 @@ the current security token. This token can be retrieved from the token storage.:
124
130
}
125
131
}
126
132
127
- In order to get the actual ``User `` object in your argument, the given value should fulfill the
128
- following requirements:
133
+ In order to get the actual ``User `` object in your argument, the given value
134
+ should fulfill the following requirements:
129
135
130
136
* The argument type (of the method signature) must be typehinted as ``User ``;
131
137
* The security token must be present;
132
138
* The value should be an instance of the ``User ``.
133
139
134
- When all those requirements are met and true is returned, the ``ArgumentResolver `` calls
135
- ``resolve() `` with the same values as it called ``supports() ``.
140
+ When all those requirements are met and true is returned, the ``ArgumentResolver ``
141
+ calls ``resolve() `` with the same values as it called ``supports() ``.
136
142
137
143
.. tip ::
138
144
139
- You can leverage the ``DefaultValueResolver `` by making your resolver accept only mandatory
140
- arguments. Given your signature is `User $user = null `, the above example will not hit ``resolve() ``
141
- as one of the conditions does not match. Eventually when the ``DefaultValueResolver `` is asked to
142
- resolve this, it will simply add the default value from the method signature, which results in ``null ``.
145
+ You can leverage the ``DefaultValueResolver `` by making your resolver
146
+ accept only mandatory arguments. Given your signature is `User $user = null `,
147
+ the above example will not hit ``resolve() `` as one of the conditions
148
+ does not match. Eventually when the ``DefaultValueResolver `` is asked
149
+ to resolve this, it will simply add the default value from the method
150
+ signature, which results in ``null ``.
143
151
144
- That's it! Now all you have to do is add the configuration for the service container. This
145
- can be done by tagging the service with ``kernel.argument_resolver `` and adding a priority.
152
+ That's it! Now all you have to do is add the configuration for the service
153
+ container. This can be done by tagging the service with ``kernel.argument_resolver ``
154
+ and adding a priority.
146
155
147
156
.. note ::
148
157
149
- While adding a priority is optional, it's recommended to add one to make sure the expected
150
- value is injected. The ``ArgumentFromAttributeResolver `` has a priority of 100. As this
151
- one is responsible for fetching attributes from the ``Request ``, it's also recommended to
152
- trigger your custom value resolver with a lower priority. This makes sure the argument
153
- resolvers are not triggered in (e.g.) subrequests if you pass your user along:
158
+ While adding a priority is optional, it's recommended to add one to
159
+ make sure the expected value is injected. The ``ArgumentFromAttributeResolver ``
160
+ has a priority of 100. As this one is responsible for fetching attributes
161
+ from the ``Request ``, it's also recommended to trigger your custom value
162
+ resolver with a lower priority. This makes sure the argument resolvers
163
+ are not triggered in (e.g.) subrequests if you pass your user along:
154
164
``{{ render(controller('AppBundle:User:index', {'user', app.user})) }} ``.
155
165
156
166
.. configuration-block ::
0 commit comments