Skip to content

Commit be779fd

Browse files
[Routing] Tell about {foo:bar} mapping syntax
1 parent 32fab7e commit be779fd

File tree

1 file changed

+32
-11
lines changed

1 file changed

+32
-11
lines changed

routing.rst

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ Creating Routes as Attributes
2222
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2323

2424
PHP attributes allow to define routes next to the code of the
25-
:doc:`controllers </controller>` associated to those routes. Attributes are
26-
native in PHP 8 and higher versions, so you can use them right away.
25+
:doc:`controllers </controller>` associated to those routes.
2726

2827
You need to add a bit of configuration to your project before using them. If your
2928
project uses :ref:`Symfony Flex <symfony-flex>`, this file is already created for you.
@@ -714,12 +713,6 @@ URL Route Parameters
714713
matches any uppercase character in any language, ``\p{Greek}`` matches any
715714
Greek characters, etc.
716715

717-
.. note::
718-
719-
When using regular expressions in route parameters, you can set the ``utf8``
720-
route option to ``true`` to make any ``.`` character match any UTF-8
721-
characters instead of just a single byte.
722-
723716
If you prefer, requirements can be inlined in each parameter using the syntax
724717
``{parameter_name<requirements>}``. This feature makes configuration more
725718
concise, but it can decrease route readability when requirements are complex:
@@ -1005,7 +998,7 @@ controller action. Instead of ``string $slug``, add ``BlogPost $post``::
1005998
{
1006999
// ...
10071000

1008-
#[Route('/blog/{slug}', name: 'blog_show')]
1001+
#[Route('/blog/{slug:post}', name: 'blog_show')]
10091002
public function show(BlogPost $post): Response
10101003
{
10111004
// $post is the object whose slug matches the routing parameter
@@ -1019,9 +1012,37 @@ this case), the "param converter" makes a database request to find the object
10191012
using the request parameters (``slug`` in this case). If no object is found,
10201013
Symfony generates a 404 response automatically.
10211014

1015+
The ``{slug:post}`` syntax maps the route parameter named ``slug`` to the controller
1016+
argument named ``$post``. It also hints the "param converter" to lookup by slug
1017+
when loading the corresponding ``BlogPost`` object from the database.
1018+
1019+
.. versionadded:: 7.1
1020+
1021+
Route parameter mapping was introduced in Symfony 7.1.
1022+
1023+
When more than one entity needs to be derived from route parameters, collisions can happen.
1024+
In the following example, the route tries to define two mappings: one to load an author by
1025+
name, two to load a category by name. But this is not allowed because from the side of the
1026+
route definition, this declares a parameter named "name" twice::
1027+
1028+
#[Route('/search-book/{name:author}/{name:category}')]
1029+
1030+
Such routes should instead be defined using the following syntax::
1031+
1032+
#[Route('/search-book/{authorName:author.name}/{categoryName:category.name}')]
1033+
1034+
This way, the route parameter names are unique (``authorName`` and ``categoryName``) and
1035+
the "param converter" can correctly map them to controller arguments (``$author`` and
1036+
``$category``), loading them both by their name.
1037+
1038+
.. versionadded:: 7.3
1039+
1040+
This more advanced style of route parameter mapping was introduced in Symfony 7.3.
1041+
1042+
More advanced mappings can be achieved using the ``#[MapEntity]`` attribute.
10221043
Check out the :ref:`Doctrine param conversion documentation <doctrine-entity-value-resolver>`
1023-
to learn about the ``#[MapEntity]`` attribute that can be used to customize the
1024-
database queries used to fetch the object from the route parameter.
1044+
to learn how to customize the database queries used to fetch the object from the route
1045+
parameter.
10251046

10261047
Backed Enum Parameters
10271048
~~~~~~~~~~~~~~~~~~~~~~

0 commit comments

Comments
 (0)