@@ -22,8 +22,7 @@ Creating Routes as Attributes
22
22
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
23
23
24
24
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.
27
26
28
27
You need to add a bit of configuration to your project before using them. If your
29
28
project uses :ref: `Symfony Flex <symfony-flex >`, this file is already created for you.
@@ -707,12 +706,6 @@ URL Route Parameters
707
706
matches any uppercase character in any language, ``\p{Greek} `` matches any
708
707
Greek characters, etc.
709
708
710
- .. note ::
711
-
712
- When using regular expressions in route parameters, you can set the ``utf8 ``
713
- route option to ``true `` to make any ``. `` character match any UTF-8
714
- characters instead of just a single byte.
715
-
716
709
If you prefer, requirements can be inlined in each parameter using the syntax
717
710
``{parameter_name<requirements>} ``. This feature makes configuration more
718
711
concise, but it can decrease route readability when requirements are complex:
@@ -998,7 +991,7 @@ controller action. Instead of ``string $slug``, add ``BlogPost $post``::
998
991
{
999
992
// ...
1000
993
1001
- #[Route('/blog/{slug}', name: 'blog_show')]
994
+ #[Route('/blog/{slug:post }', name: 'blog_show')]
1002
995
public function show(BlogPost $post): Response
1003
996
{
1004
997
// $post is the object whose slug matches the routing parameter
@@ -1012,9 +1005,37 @@ this case), the "param converter" makes a database request to find the object
1012
1005
using the request parameters (``slug `` in this case). If no object is found,
1013
1006
Symfony generates a 404 response automatically.
1014
1007
1008
+ The ``{slug:post} `` syntax maps the route parameter named ``slug `` to the controller
1009
+ argument named ``$blog ``. It also hints the "param converter" to lookup by slug
1010
+ when loading the corresponding ``BlogPost `` object from the database.
1011
+
1012
+ .. versionadded :: 7.1
1013
+
1014
+ Route parameter mapping was introduced in Symfony 7.1.
1015
+
1016
+ When more than one entity needs to be derived from route parameters, collisions can happen.
1017
+ In the following example, the route tries to define two mappings: one to load an author by
1018
+ name, two to load a category by name. But this is not allowed because from the side of the
1019
+ route definition, this declares a parameter named "name" twice::
1020
+
1021
+ #[Route('/search-book/{name:author}/{name:category}')]
1022
+
1023
+ Such routes should instead be defined using the following syntax::
1024
+
1025
+ #[Route('/search-book/{authorName:author.name}/{categoryName:category.name}')]
1026
+
1027
+ This way, the route parameter names are unique (``authorName `` and ``categoryName ``) and
1028
+ the "param converter" can correctly map them to controller arguments (``$author `` and
1029
+ ``$category ``), loading them both by their name.
1030
+
1031
+ .. versionadded :: 7.3
1032
+
1033
+ This more advanced style of route parameter mapping was introduced in Symfony 7.3.
1034
+
1035
+ More advanced mappings can be achieved using the ``#[MapEntity] `` attribute.
1015
1036
Check out the :ref: `Doctrine param conversion documentation <doctrine-entity-value-resolver >`
1016
- to learn about the `` #[MapEntity] `` attribute that can be used to customize the
1017
- database queries used to fetch the object from the route parameter.
1037
+ to learn how to customize the database queries used to fetch the object from the route
1038
+ parameter.
1018
1039
1019
1040
Backed Enum Parameters
1020
1041
~~~~~~~~~~~~~~~~~~~~~~
0 commit comments