-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Added docs about ArgumentValueResolvers #6438
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
7fc9fe8
7ddf5d6
1a19d2e
f491199
a07fcc3
2e41c07
7cdc96b
7d00d8c
f9cbe71
4c6ed2a
9784406
e3d1b48
55a87b5
8d30575
dd225e8
ca014a6
14d77e1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,20 +17,21 @@ Functionality Shipped With The HttpKernel | |
----------------------------------------- | ||
|
||
Symfony ships with four value resolvers in the HttpKernel: | ||
- The :class:`Symfony\\Component\\HttpKernel\\Controller\\ArgumentValueResolver\\ArgumentFromAttributeResolver` | ||
* The :class:`Symfony\\Component\\HttpKernel\\Controller\\ArgumentValueResolver\\ArgumentFromAttributeResolver` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please add an empty line (and remove the indentation for lists here as well) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. actually, use a definition list here: :class:`Symfony\\Component\\HttpKernel\\Controller\\ArgumentValueResolver\\ArgumentFromAttributeResolver`
Attempts to find a request attribute that matches the name of the argument.
:class:`Symfony\\Component\\HttpKernel\\Controller\\ArgumentValueResolver\\RequestValueResolver`
Injects the current `Request` if an argument typehinted for `Request` or sub-class.
[...] |
||
attempts to find a request attribute that matches the name of the argument. | ||
|
||
- The :class:`Symfony\\Component\\HttpKernel\\Controller\\ArgumentValueResolver\\RequestValueResolver` | ||
* The :class:`Symfony\\Component\\HttpKernel\\Controller\\ArgumentValueResolver\\RequestValueResolver` | ||
injects the current ``Request`` if type-hinted with ``Request``, or a sub-class thereof. | ||
|
||
- The :class:`Symfony\\Component\\HttpKernel\\Controller\\ArgumentValueResolver\\DefaultValueResolver` | ||
* The :class:`Symfony\\Component\\HttpKernel\\Controller\\ArgumentValueResolver\\DefaultValueResolver` | ||
will set the default value of the argument if present and the argument is optional. | ||
|
||
- The :class:`Symfony\\Component\\HttpKernel\\Controller\\ArgumentValueResolver\\VariadicValueResolver` | ||
* The :class:`Symfony\\Component\\HttpKernel\\Controller\\ArgumentValueResolver\\VariadicValueResolver` | ||
verifies in the request if your data is an array and will add all of them to the argument list. | ||
When the action is called, the last (variadic) argument will contain all the values of this array. | ||
|
||
.. note:: | ||
|
||
In older versions of Symfony this logic was all resolved within the ``ControllerResolver``. The | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "In older versions of Symfony" -> "Prior to Symfony 3.1, this logic was resolved within the |
||
old functionality is moved to the ``LegacyArgumentResolver``, which contains the previously | ||
used resolving logic. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If #18495 is merged, the legacy class won't exist anymore. |
||
|
@@ -39,7 +40,7 @@ Adding a New Value Resolver | |
--------------------------- | ||
|
||
Adding a new value resolver requires one class and one service defintion. In our next example, we | ||
will be creating a shortcut to inject the ``User`` object from our security. Given we write the following | ||
will be creating a shortcut to inject the ``User`` object from our security. Given you write the following | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "In the next example, you'll create a value resolver to inject the |
||
action:: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "action" -> "controller" |
||
|
||
namespace AppBundle\Controller; | ||
|
@@ -52,37 +53,38 @@ action:: | |
} | ||
} | ||
|
||
Somehow we will have to get the ``User`` object and inject it into our action. This can be done | ||
Somehow you will have to get the ``User`` object and inject it into our action. This can be done | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "our action" -> "the controller" |
||
by implementing the :class:`Symfony\\Component\\HttpKernel\\Controller\\ArgumentValueResolverInterface`. | ||
This interface specifies that we have to implement two methods:: | ||
This interface specifies that you have to implement two methods:: | ||
|
||
interface ArgumentValueResolverInterface | ||
{ | ||
public function supports(Request $request, ArgumentMetadata $argument); | ||
public function resolve(Request $request, ArgumentMetadata $argument); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would remove the PHP code of the interface. |
||
|
||
- The ``supports()`` method is used to check whether the resolver supports the given argument. It will | ||
* The ``supports()`` method is used to check whether the resolver supports the given argument. It will | ||
only continue if it returns ``true``. | ||
|
||
- The ``resolve()`` method will be used to resolve the actual value just acknowledged by | ||
* The ``resolve()`` method will be used to resolve the actual value just acknowledged by | ||
``supports()``. Once a value is resolved you can ``yield`` the value to the ``ArgumentResolver``. | ||
|
||
- The ``Request`` object is the current ``Request`` which would also be injected into your | ||
* The ``Request`` object is the current ``Request`` which would also be injected into your | ||
action in the forementioned functionality. | ||
|
||
- The :class:``Symfony\\Component\\HttpKernel\\ControllerMetadata\\ArgumentMetadata`` represents | ||
* The :class:``Symfony\\Component\\HttpKernel\\ControllerMetadata\\ArgumentMetadata`` represents | ||
information retrieved from the method signature for the current argument it's trying to resolve. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lists shouldn't be indented with one space in reSt, it'll end up as a blockquote. Also, please use |
||
|
||
.. note:: | ||
|
||
The ``ArgumentMetadata`` is a simple data container created by the | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. there should be an empty line between the directive start ( |
||
:class:``Symfony\\Component\\HttpKernel\\ControllerMetadata\\ArgumentMetadataFactory``. This | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the double backticks should be single ones (as this is a role and not a literal) |
||
factory will work on every supported php version but might give different results. E.g. the | ||
``isVariadic()`` will never return true on php 5.5 and only on php 7.0 and higher it will give | ||
factory will work on every supported PHP version but might give different results. E.g. the | ||
``isVariadic()`` will never return true on PHP 5.5 and only on PHP 7.0 and higher it will give | ||
you basic types when calling ``getType()``. | ||
|
||
Now that we know what to do, we can implement this interface. In order to get the current ``User``, | ||
we will have to get it from the ``TokenInterface`` which is in the ``TokenStorageInterface``:: | ||
Now that you know what to do, you can implement this interface. In order to get the current ``User``, | ||
you will have to get it from the ``TokenInterface`` which is in the ``TokenStorageInterface``:: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's split this long sentence in a couple smaller ones to make it easier to read: "To get the current There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've removed "current" from the last sentence. This because there's only 1 storage. |
||
|
||
namespace AppBundle\ArgumentValueResolver; | ||
|
||
|
@@ -110,16 +112,17 @@ we will have to get it from the ``TokenInterface`` which is in the ``TokenStorag | |
} | ||
} | ||
|
||
This was pretty simple, now all we have to do is add the configuration for the service container. This | ||
This was pretty simple, now all you have to do is add the configuration for the service container. This | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Some people don't like using "simple" etc. in documentations, as things can be a lot harder for beginners than you imagine. Maybe replace "This was pretty simple" with "That's it!" to be a bit more neutral. |
||
can be done by tagging the service with ``kernel.argument_resolver`` and adding a priority. | ||
|
||
.. note:: | ||
|
||
While adding a priority is optional, it's recommended to add one to make sure the expected | ||
value is injected. The ``ArgumentFromAttributeResolver`` has a priority of 100. As this | ||
one is responsible for fetching attributes from the ``Request``, it's also recommended to | ||
trigger your custom value resolver with a lower priority. This makes sure the argument | ||
resolvers are not triggered in (e.g.) subrequests if you pass your user along: | ||
``{{ render(controller('AppBundle:User:index', {'user', app.user})) }}``. | ||
``{{ render(controller('AppBundle:User:index', {'user', app.user})) }}``. | ||
|
||
.. configuration-block:: | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[...] in the HttpKernel component