-
-
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
Closed
Closed
Changes from 1 commit
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
7fc9fe8
Added docs about ArgumentValueResolvers
7ddf5d6
we>you, php>PHP, fixed directive start & list style
1a19d2e
Fixed the list indents
f491199
Added missing use statements to example
a07fcc3
Processed feedback from @wouterj
2e41c07
Added some extra info about the DefaultValueResolver
7cdc96b
Fixed a typo in code example
7d00d8c
Fixed a wrong tag name
f9cbe71
Added instanceof check in example
4c6ed2a
Respecting the line length
9784406
Fixed the tag name
e3d1b48
Tried to make the default value resolving more clear
55a87b5
Minor fixes
8d30575
Added instance
dd225e8
Feedback from PR
ca014a6
Fixed sub-title case
14d77e1
typehinted > type-hinted
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Added some extra info about the DefaultValueResolver
- Loading branch information
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
----------------------------------------- | ||
|
||
|
@@ -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; | ||
|
@@ -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`. | ||
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 add "instance" in this sentence: "Both methods get [...] and an |
||
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:: | ||
|
@@ -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``. | ||
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 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. | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
with
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.
Shouldn't it be something like this then:
Functionality Shipped with the HttpKernel
? It looks odd to havewith
butThe
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.
You're right.