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
Prev Previous commit
Next Next commit
Feedback from PR
  • Loading branch information
Iltar van der Berg committed May 17, 2016
commit dd225e8cbbaccdfaebeceb774ffd8a06c31e30fe
23 changes: 4 additions & 19 deletions cookbook/controller/argument_value_resolver.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ this functionality.
Functionality Shipped With 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.

with

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Shouldn't it be something like this then: Functionality Shipped with the HttpKernel? It looks odd to have with but The

Copy link
Member

Choose a reason for hiding this comment

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

You're right.

-----------------------------------------

Symfony ships with four value resolvers in the HttpKernel:
Symfony ships with four value resolvers in the HttpKernel component:

:class:`Symfony\\Component\\HttpKernel\\Controller\\ArgumentValueResolver\\ArgumentFromAttributeResolver`
Attempts to find a request attribute that matches the name of the argument.
Expand Down Expand Up @@ -62,13 +62,7 @@ object from the security system. Given you write the following action::

Somehow you will have to get the ``User`` object and inject it into the controller.
This can be done by implementing the :class:`Symfony\\Component\\HttpKernel\\Controller\\ArgumentValueResolverInterface`.
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);
}
This interface specifies that you have to implement two methods:

``supports()``
This method is used to check whether the value resolver supports the
Expand All @@ -82,15 +76,6 @@ Both methods get the ``Request`` object, which is the current request, and an
instance. This object contains all information retrieved from the method signature
for the current argument.

.. note::

The ``ArgumentMetadata`` is a simple data container created by the
:class:`Symfony\\Component\\HttpKernel\\ControllerMetadata\\ArgumentMetadataFactory`.
This 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 you know what to do, you can implement this interface. To get the
current ``User``, you need the current security token. This token can be
retrieved from the token storage::
Expand Down Expand Up @@ -134,15 +119,15 @@ retrieved from the token storage::
In order to get the actual ``User`` object in your argument, the given value
must fulfill the following requirements:

* An argument must be typehinted as ``User`` in your action method signature;
* An argument must be type-hinted as ``User`` in your action method signature;
* A security token must be present;
* The value must be an instance of the ``User``.

When all those requirements are met and true is returned, the ``ArgumentResolver``
calls ``resolve()`` with the same values as it called ``supports()``.

That's it! Now all you have to do is add the configuration for the service
container. This can be done by tagging the service with ``kernel.argument_resolver``
container. This can be done by tagging the service with ``controller.argument_resolver``
and adding a priority.

.. note::
Expand Down