Skip to content

Commit 4a93223

Browse files
committed
Merge branch '7.2' into 7.3
* 7.2: Minor tweaks [Routing] Tell about {foo:bar} mapping syntax
2 parents b36bab2 + 88a7eb2 commit 4a93223

File tree

2 files changed

+24
-31
lines changed

2 files changed

+24
-31
lines changed

doctrine.rst

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -680,7 +680,7 @@ will automatically fetch them::
680680
/**
681681
* Perform a findOneBy() where the slug property matches {slug}.
682682
*/
683-
#[Route('/product/{slug}')]
683+
#[Route('/product/{slug:product}')]
684684
public function showBySlug(Product $product): Response
685685
{
686686
}
@@ -694,14 +694,17 @@ Automatic fetching works in these situations:
694694
*all* of the wildcards in your route that are actually properties
695695
on your entity (non-properties are ignored).
696696

697-
This behavior is enabled by default on all controllers. If you prefer, you can
698-
restrict this feature to only work on route wildcards called ``id`` to look for
699-
entities by primary key. To do so, set the option
700-
``doctrine.orm.controller_resolver.auto_mapping`` to ``false``.
697+
The ``{slug:product}`` syntax maps the route parameter named ``slug`` to the
698+
controller argument named ``$product``. It also hints the resolver to look up
699+
the corresponding ``Product`` object from the database using the slug.
701700

702-
When ``auto_mapping`` is disabled, you can configure the mapping explicitly for
703-
any controller argument with the ``MapEntity`` attribute. You can even control
704-
the ``EntityValueResolver`` behavior by using the `MapEntity options`_ ::
701+
.. versionadded:: 7.1
702+
703+
Route parameter mapping was introduced in Symfony 7.1.
704+
705+
You can also configure the mapping explicitly for any controller argument
706+
using the ``MapEntity`` attribute. You can even control the behavior of the
707+
``EntityValueResolver`` by using the `MapEntity options`_ ::
705708

706709
// src/Controller/ProductController.php
707710
namespace App\Controller;
@@ -812,18 +815,6 @@ control behavior:
812815
): Response {
813816
}
814817

815-
``exclude``
816-
Configures the properties that should be used in the ``findOneBy()``
817-
method by *excluding* one or more properties so that not *all* are used::
818-
819-
#[Route('/product/{slug}/{date}')]
820-
public function show(
821-
#[MapEntity(exclude: ['date'])]
822-
Product $product,
823-
\DateTime $date
824-
): Response {
825-
}
826-
827818
``stripNull``
828819
If true, then when ``findOneBy()`` is used, any values that are
829820
``null`` will not be used for the query.

routing.rst

Lines changed: 13 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,18 @@ 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 look up the
1017+
corresponding ``BlogPost`` object from the database using the slug.
1018+
1019+
.. versionadded:: 7.1
1020+
1021+
Route parameter mapping was introduced in Symfony 7.1.
1022+
1023+
More advanced mappings can be achieved using the ``#[MapEntity]`` attribute.
10221024
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.
1025+
to learn how to customize the database queries used to fetch the object from the route
1026+
parameter.
10251027

10261028
Backed Enum Parameters
10271029
~~~~~~~~~~~~~~~~~~~~~~

0 commit comments

Comments
 (0)