Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork5.2k
[Routing] Tell about{foo:bar}
mapping syntax#20956
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -680,7 +680,7 @@ will automatically fetch them:: | ||
/** | ||
* Perform a findOneBy() where the slug property matches {slug}. | ||
*/ | ||
#[Route('/product/{slug:product}')] | ||
public function showBySlug(Product $product): Response | ||
{ | ||
} | ||
@@ -694,14 +694,17 @@ Automatic fetching works in these situations: | ||
*all* of the wildcards in your route that are actually properties | ||
on your entity (non-properties are ignored). | ||
The ``{slug:product}`` syntax maps the route parameter named ``slug`` to the | ||
controller argument named ``$product``. It also hints the resolver to lookup | ||
by slug when loading the corresponding ``Product`` object from the database. | ||
.. versionadded:: 7.1 | ||
Route parameter mapping was introduced in Symfony 7.1. | ||
You can also configure the mapping explicitly for any controller argument | ||
with the ``MapEntity`` attribute. You can even control the | ||
``EntityValueResolver`` behavior by using the `MapEntity options`_ :: | ||
// src/Controller/ProductController.php | ||
namespace App\Controller; | ||
@@ -812,18 +815,6 @@ control behavior: | ||
): Response { | ||
} | ||
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's no use case for | ||
``stripNull`` | ||
If true, then when ``findOneBy()`` is used, any values that are | ||
``null`` will not be used for the query. | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -22,8 +22,7 @@ | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
PHP attributes allow to define routes next to the code of the | ||
:doc:`controllers </controller>` associated to those routes. | ||
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. PHP 8 is the past now, the removed sentence feels outdated | ||
You need to add a bit of configuration to your project before using them. If your | ||
project uses :ref:`Symfony Flex <symfony-flex>`, this file is already created for you. | ||
@@ -707,12 +706,6 @@ | ||
matches any uppercase character in any language, ``\p{Greek}`` matches any | ||
Greek characters, etc. | ||
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. utf8 is enabled by default since a few years now | ||
If you prefer, requirements can be inlined in each parameter using the syntax | ||
``{parameter_name<requirements>}``. This feature makes configuration more | ||
concise, but it can decrease route readability when requirements are complex: | ||
@@ -998,7 +991,7 @@ | ||
{ | ||
// ... | ||
#[Route('/blog/{slug:post}', name: 'blog_show')] | ||
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 example is using deprecated implicit mapping that doesn't work anymore with the default recipes since 7.1 | ||
public function show(BlogPost $post): Response | ||
{ | ||
// $post is the object whose slug matches the routing parameter | ||
@@ -1012,9 +1005,37 @@ | ||
using the request parameters (``slug`` in this case). If no object is found, | ||
Symfony generates a 404 response automatically. | ||
The ``{slug:post}`` syntax maps the route parameter named ``slug`` to the controller | ||
argument named ``$post``. It also hints the "param converter" to lookup by slug | ||
when loading the corresponding ``BlogPost`` object from the database. | ||
.. versionadded:: 7.1 | ||
Route parameter mapping was introduced in Symfony 7.1. | ||
When more than one entity needs to be derived from route parameters, collisions can happen. | ||
In the following example, the route tries to define two mappings: one to load an author by | ||
name, two to load a category by name. But this is not allowed because from the side of the | ||
route definition, this declares a parameter named "name" twice:: | ||
#[Route('/search-book/{name:author}/{name:category}')] | ||
Such routes should instead be defined using the following syntax:: | ||
#[Route('/search-book/{authorName:author.name}/{categoryName:category.name}')] | ||
This way, the route parameter names are unique (``authorName`` and ``categoryName``) and | ||
the "param converter" can correctly map them to controller arguments (``$author`` and | ||
``$category``), loading them both by their name. | ||
.. versionadded:: 7.3 | ||
This more advanced style of route parameter mapping was introduced in Symfony 7.3. | ||
nicolas-grekas marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
More advanced mappings can be achieved using the ``#[MapEntity]`` attribute. | ||
Check out the :ref:`Doctrine param conversion documentation <doctrine-entity-value-resolver>` | ||
to learnhow to customize the database queriesused tofetch the object from the route | ||
parameter. | ||
Backed Enum Parameters | ||
~~~~~~~~~~~~~~~~~~~~~~ | ||
Uh oh!
There was an error while loading.Please reload this page.