@@ -17,20 +17,21 @@ Functionality Shipped With The HttpKernel
17
17
-----------------------------------------
18
18
19
19
Symfony ships with four value resolvers in the HttpKernel:
20
- - The :class: `Symfony\\ Component\\ HttpKernel\\ Controller\\ ArgumentValueResolver\\ ArgumentFromAttributeResolver `
20
+ * The :class: `Symfony\\ Component\\ HttpKernel\\ Controller\\ ArgumentValueResolver\\ ArgumentFromAttributeResolver `
21
21
attempts to find a request attribute that matches the name of the argument.
22
22
23
- - The :class: `Symfony\\ Component\\ HttpKernel\\ Controller\\ ArgumentValueResolver\\ RequestValueResolver `
23
+ * The :class: `Symfony\\ Component\\ HttpKernel\\ Controller\\ ArgumentValueResolver\\ RequestValueResolver `
24
24
injects the current ``Request `` if type-hinted with ``Request ``, or a sub-class thereof.
25
25
26
- - The :class: `Symfony\\ Component\\ HttpKernel\\ Controller\\ ArgumentValueResolver\\ DefaultValueResolver `
26
+ * The :class: `Symfony\\ Component\\ HttpKernel\\ Controller\\ ArgumentValueResolver\\ DefaultValueResolver `
27
27
will set the default value of the argument if present and the argument is optional.
28
28
29
- - The :class: `Symfony\\ Component\\ HttpKernel\\ Controller\\ ArgumentValueResolver\\ VariadicValueResolver `
29
+ * The :class: `Symfony\\ Component\\ HttpKernel\\ Controller\\ ArgumentValueResolver\\ VariadicValueResolver `
30
30
verifies in the request if your data is an array and will add all of them to the argument list.
31
31
When the action is called, the last (variadic) argument will contain all the values of this array.
32
32
33
33
.. note ::
34
+
34
35
In older versions of Symfony this logic was all resolved within the ``ControllerResolver ``. The
35
36
old functionality is moved to the ``LegacyArgumentResolver ``, which contains the previously
36
37
used resolving logic.
@@ -39,7 +40,7 @@ Adding a New Value Resolver
39
40
---------------------------
40
41
41
42
Adding a new value resolver requires one class and one service defintion. In our next example, we
42
- will be creating a shortcut to inject the ``User `` object from our security. Given we write the following
43
+ will be creating a shortcut to inject the ``User `` object from our security. Given you write the following
43
44
action::
44
45
45
46
namespace AppBundle\Controller;
@@ -52,37 +53,38 @@ action::
52
53
}
53
54
}
54
55
55
- Somehow we will have to get the ``User `` object and inject it into our action. This can be done
56
+ Somehow you will have to get the ``User `` object and inject it into our action. This can be done
56
57
by implementing the :class: `Symfony\\ Component\\ HttpKernel\\ Controller\\ ArgumentValueResolverInterface `.
57
- This interface specifies that we have to implement two methods::
58
+ This interface specifies that you have to implement two methods::
58
59
59
60
interface ArgumentValueResolverInterface
60
61
{
61
62
public function supports(Request $request, ArgumentMetadata $argument);
62
63
public function resolve(Request $request, ArgumentMetadata $argument);
63
64
}
64
65
65
- - The ``supports()`` method is used to check whether the resolver supports the given argument. It will
66
+ * The ``supports()`` method is used to check whether the resolver supports the given argument. It will
66
67
only continue if it returns ``true``.
67
68
68
- - The ``resolve()`` method will be used to resolve the actual value just acknowledged by
69
+ * The ``resolve()`` method will be used to resolve the actual value just acknowledged by
69
70
``supports()``. Once a value is resolved you can ``yield`` the value to the ``ArgumentResolver``.
70
71
71
- - The ``Request`` object is the current ``Request`` which would also be injected into your
72
+ * The ``Request`` object is the current ``Request`` which would also be injected into your
72
73
action in the forementioned functionality.
73
74
74
- - The :class:``Symfony\\Component\\HttpKernel\\ControllerMetadata\\ArgumentMetadata`` represents
75
+ * The :class:``Symfony\\Component\\HttpKernel\\ControllerMetadata\\ArgumentMetadata`` represents
75
76
information retrieved from the method signature for the current argument it's trying to resolve.
76
77
77
78
.. note ::
79
+
78
80
The ``ArgumentMetadata `` is a simple data container created by the
79
81
:class: ``Symfony\\ Component\\ HttpKernel\\ ControllerMetadata\\ ArgumentMetadataFactory` `. This
80
- factory will work on every supported php version but might give different results. E.g. the
81
- ``isVariadic() `` will never return true on php 5.5 and only on php 7.0 and higher it will give
82
+ factory will work on every supported PHP version but might give different results. E.g. the
83
+ ``isVariadic() `` will never return true on PHP 5.5 and only on PHP 7.0 and higher it will give
82
84
you basic types when calling ``getType() ``.
83
85
84
- Now that we know what to do, we can implement this interface. In order to get the current ``User ``,
85
- we will have to get it from the ``TokenInterface `` which is in the ``TokenStorageInterface ``::
86
+ Now that you know what to do, you can implement this interface. In order to get the current ``User ``,
87
+ you will have to get it from the ``TokenInterface `` which is in the ``TokenStorageInterface ``::
86
88
87
89
namespace AppBundle\ArgumentValueResolver;
88
90
@@ -110,16 +112,17 @@ we will have to get it from the ``TokenInterface`` which is in the ``TokenStorag
110
112
}
111
113
}
112
114
113
- This was pretty simple, now all we have to do is add the configuration for the service container. This
115
+ This was pretty simple, now all you have to do is add the configuration for the service container. This
114
116
can be done by tagging the service with ``kernel.argument_resolver `` and adding a priority.
115
117
116
118
.. note ::
119
+
117
120
While adding a priority is optional, it's recommended to add one to make sure the expected
118
121
value is injected. The ``ArgumentFromAttributeResolver `` has a priority of 100. As this
119
122
one is responsible for fetching attributes from the ``Request ``, it's also recommended to
120
123
trigger your custom value resolver with a lower priority. This makes sure the argument
121
124
resolvers are not triggered in (e.g.) subrequests if you pass your user along:
122
- ``{{ render(controller('AppBundle:User:index', {'user', app.user})) }} ``.
125
+ ``{{ render(controller('AppBundle:User:index', {'user', app.user})) }} ``.
123
126
124
127
.. configuration-block ::
125
128
0 commit comments