Skip to content

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

Closed
Closed
Prev Previous commit
Next Next commit
we>you, php>PHP, fixed directive start & list style
  • Loading branch information
Iltar van der Berg committed Apr 29, 2016
commit 7ddf5d67a5dce3253e4b308b19dcd3ff145f41b3
37 changes: 20 additions & 17 deletions cookbook/controller/argument_value_resolver.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,21 @@ Functionality Shipped With The HttpKernel
-----------------------------------------

Symfony ships with four value resolvers in the HttpKernel:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[...] in the HttpKernel component

- The :class:`Symfony\\Component\\HttpKernel\\Controller\\ArgumentValueResolver\\ArgumentFromAttributeResolver`
* The :class:`Symfony\\Component\\HttpKernel\\Controller\\ArgumentValueResolver\\ArgumentFromAttributeResolver`
Copy link
Member

Choose a reason for hiding this comment

The 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)

Copy link
Member

Choose a reason for hiding this comment

The 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
Copy link
Member

Choose a reason for hiding this comment

The 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 ControllerResolver. [...]"

old functionality is moved to the ``LegacyArgumentResolver``, which contains the previously
used resolving logic.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If #18495 is merged, the legacy class won't exist anymore.

Expand All @@ -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
Copy link
Member

Choose a reason for hiding this comment

The 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 User object from the security system."

action::
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"action" -> "controller"


namespace AppBundle\Controller;
Expand All @@ -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
Copy link
Member

Choose a reason for hiding this comment

The 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);
}
Copy link
Member

Choose a reason for hiding this comment

The 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.
Copy link
Member

Choose a reason for hiding this comment

The 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 * to be consistent with the other lists in the documentation.


.. note::

The ``ArgumentMetadata`` is a simple data container created by the
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there should be an empty line between the directive start (.. note::) and the body (except from the versionadded directive)

:class:``Symfony\\Component\\HttpKernel\\ControllerMetadata\\ArgumentMetadataFactory``. This
Copy link
Member

Choose a reason for hiding this comment

The 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``::
Copy link
Member

Choose a reason for hiding this comment

The 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 User, you need the current security token. This token can be retrieved from the current token storage."

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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;

Expand Down Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

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

Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy