Skip to content

Commit bf50e16

Browse files
author
Iltar van der Berg
committed
Respecting the line length
1 parent 6358f47 commit bf50e16

File tree

1 file changed

+47
-37
lines changed

1 file changed

+47
-37
lines changed

cookbook/controller/argument_value_resolver.rst

Lines changed: 47 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ Extending Action Argument Resolving
88
The ``ArgumentResolver`` and value resolvers are added in Symfony 3.1.
99

1010
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.
1415

1516
Functionality Shipped With The HttpKernel
1617
-----------------------------------------
@@ -21,26 +22,29 @@ Symfony ships with four value resolvers in the HttpKernel:
2122
Attempts to find a request attribute that matches the name of the argument.
2223

2324
: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.
2527

2628
: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.
2831

2932
: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.
3236

3337
.. note::
3438

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.
3741

3842
Adding a Custom Value Resolver
3943
------------------------------
4044

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::
4448

4549
namespace AppBundle\Controller;
4650

@@ -55,8 +59,8 @@ you write the following action::
5559
}
5660
}
5761

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`.
6064
This interface specifies that you have to implement two methods::
6165

6266
interface ArgumentValueResolverInterface
@@ -74,19 +78,21 @@ This interface specifies that you have to implement two methods::
7478

7579
Both methods get the ``Request`` object, which is the current request, and an
7680
: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.
7983

8084
.. note::
8185

8286
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()``.
8792

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::
9096

9197
namespace AppBundle\ArgumentValueResolver;
9298

@@ -124,33 +130,37 @@ the current security token. This token can be retrieved from the token storage.:
124130
}
125131
}
126132

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:
129135

130136
* The argument type (of the method signature) must be typehinted as ``User``;
131137
* The security token must be present;
132138
* The value should be an instance of the ``User``.
133139

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()``.
136142

137143
.. tip::
138144

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``.
143151

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.
146155

147156
.. note::
148157

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:
154164
``{{ render(controller('AppBundle:User:index', {'user', app.user})) }}``.
155165

156166
.. configuration-block::

0 commit comments

Comments
 (0)