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
Added some extra info about the DefaultValueResolver
  • Loading branch information
Iltar van der Berg committed Apr 29, 2016
commit 2e41c0752440a6f088267bde21c2ac68b89aad91
25 changes: 20 additions & 5 deletions cookbook/controller/argument_value_resolver.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ object by adding a ``Request`` argument to your controller. This is done via the
:class:`Symfony\\Component\\HttpKernel\\Controller\\ArgumentResolver`. By creating and registering custom
argument value resolvers, you can extend 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.

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

Expand All @@ -34,14 +33,13 @@ Symfony ships with four value resolvers in the HttpKernel:
.. note::

Prior to Symfony 3.1, this logic was resolved within the ``ControllerResolver``. The old
functionality is moved to the ``LegacyArgumentResolver``, which contains the previously
used resolving logic.
functionality is rewritten to the aforementioned value resolvers.

Adding a Custom Value Resolver
------------------------------

Adding a new value resolver requires one class and one service defintion. In the next example,
you'll create a value resolver to inject the ``User`` object from the security system.. Given
you'll create a value resolver to inject the ``User`` object from the security system. Given
you write the following action::

namespace AppBundle\Controller;
Expand Down Expand Up @@ -76,7 +74,7 @@ This interface specifies that you have to implement two methods::

Both methods get the ``Request`` object, which is the current request, and an
:class:`Symfony\\Component\\HttpKernel\\ControllerMetadata\\ArgumentMetadata`.
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 add "instance" in this sentence: "Both methods get [...] and an ArgumentMetadata instance"

This object contains all informations retrieved from the method signature for the
This object contains all information retrieved from the method signature for the
current argument.

.. note::
Expand Down Expand Up @@ -121,6 +119,23 @@ the current security token. This token can be retrieved from the token storage.:
}
}

In order to get the actual ``User`` object in your argument, the given value should fulfill the
following requirements:

* The argument type (of the method signature) must be typehinted as ``User``;
* The security token must be present;
* The value should be an instance of the ``User``.
Copy link
Contributor

Choose a reason for hiding this comment

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

I think you can remove the "the" :)


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

.. tip::

You can leverage the ``DefaultValueResolver`` by making your resolver accept only mandatory
arguments. Given your signature is `User $user = null`, the above example will not hit ``resolve()``
as one of the conditions does not match. Eventually when the ``DefaultValueResolver`` is asked to
resolve this, it will simply add the default value from the method signature, which results in ``null``.

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`` and adding a priority.

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