From a137136b3aa0b9e93b275842c960f462573dd3e0 Mon Sep 17 00:00:00 2001 From: Antoine Lamirault Date: Tue, 7 May 2024 15:15:04 +0200 Subject: [PATCH 1/3] [Routing] Add {foo:bar} syntax to define a mapping between a route parameter and its corresponding request attribute --- routing.rst | 130 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 130 insertions(+) diff --git a/routing.rst b/routing.rst index 790ae314516..abb797dc863 100644 --- a/routing.rst +++ b/routing.rst @@ -995,6 +995,136 @@ convert them automatically to their scalar values. } } +Mapping Parameters +~~~~~~~~~~~~~~~~~~ + +By default, the name of the variable part (``{slug}`` for example) is the +argument injected name to the method (``$slug``). + +You can change this behavior and define mapping between variable part and +argument name with ``{variable_part_name:argument_name}``: + +.. configuration-block:: + + .. code-block:: php-attributes + + // src/Controller/BlogController.php + namespace App\Controller; + + use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; + use Symfony\Component\HttpFoundation\Response; + use Symfony\Component\Routing\Attribute\Route; + + class BlogController extends AbstractController + { + // ... + + #[Route('/blog/{slug:article}', name: 'blog_show')] + public function show(string $article): Response + { + // $article will equal the dynamic part of the URL + // e.g. at /blog/yay-routing, then $article='yay-routing' + + // ... + } + } + + .. code-block:: yaml + + # config/routes.yaml + blog_show: + path: /blog/{slug:article} + controller: App\Controller\BlogController::show + + .. code-block:: xml + + + + + + + + + .. code-block:: php + + // config/routes.php + use App\Controller\BlogController; + use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator; + + return function (RoutingConfigurator $routes): void { + $routes->add('blog_show', '/blog/{slug:article}') + ->controller([BlogController::class, 'show']) + ; + }; + +When two or more variable parts target the same argument name, argument will be +an array: + +.. configuration-block:: + + .. code-block:: php-attributes + + // src/Controller/BlogController.php + namespace App\Controller; + + use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; + use Symfony\Component\HttpFoundation\Response; + use Symfony\Component\Routing\Attribute\Route; + + class BlogController extends AbstractController + { + // ... + + #[Route('/blog/{id:article}/{slug:article}', name: 'blog_show')] + public function show(array $article): Response + { + // $article will equal the dynamic part of the URL + // e.g. at /blog/12/yay-routing, then $article=['id' => '12', 'slug' => 'yay-routing'] + + // ... + } + } + + .. code-block:: yaml + + # config/routes.yaml + blog_show: + path: /blog/{id:article}/{slug:article} + controller: App\Controller\BlogController::show + + .. code-block:: xml + + + + + + + + + .. code-block:: php + + // config/routes.php + use App\Controller\BlogController; + use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator; + + return function (RoutingConfigurator $routes): void { + $routes->add('blog_show', '/blog/{id:article}/{slug:article}') + ->controller([BlogController::class, 'show']) + ; + }; + +.. versionadded:: 7.1 + + The mapping of route parameters was introduced in Symfony 7.1. + Special Parameters ~~~~~~~~~~~~~~~~~~ From 534aac1ebe05fb32aa1e50dd5b067a1a9d2d8e1b Mon Sep 17 00:00:00 2001 From: Antoine Lamirault Date: Wed, 8 May 2024 10:27:26 +0200 Subject: [PATCH 2/3] Apply suggestions from code review Co-authored-by: Oskar Stark --- routing.rst | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/routing.rst b/routing.rst index abb797dc863..f705f76d687 100644 --- a/routing.rst +++ b/routing.rst @@ -1001,8 +1001,8 @@ Mapping Parameters By default, the name of the variable part (``{slug}`` for example) is the argument injected name to the method (``$slug``). -You can change this behavior and define mapping between variable part and -argument name with ``{variable_part_name:argument_name}``: +You can change this behavior and define a mapping between variable part and +an argument name with ``{variable_part_name:argument_name}``: .. configuration-block:: @@ -1022,7 +1022,7 @@ argument name with ``{variable_part_name:argument_name}``: #[Route('/blog/{slug:article}', name: 'blog_show')] public function show(string $article): Response { - // $article will equal the dynamic part of the URL + // $article will be equal to the dynamic part of the URL // e.g. at /blog/yay-routing, then $article='yay-routing' // ... From dab23282b9fa7b466c9d0205a6b2f36b7851a889 Mon Sep 17 00:00:00 2001 From: Antoine Lamirault Date: Thu, 9 May 2024 20:31:27 +0200 Subject: [PATCH 3/3] Fix CR issue --- routing.rst | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/routing.rst b/routing.rst index f705f76d687..4d942793218 100644 --- a/routing.rst +++ b/routing.rst @@ -998,11 +998,11 @@ convert them automatically to their scalar values. Mapping Parameters ~~~~~~~~~~~~~~~~~~ -By default, the name of the variable part (``{slug}`` for example) is the -argument injected name to the method (``$slug``). +By default, the route parameter (``{slug}`` for example) is the name of the argument +injected to the controller method (``$slug``). -You can change this behavior and define a mapping between variable part and -an argument name with ``{variable_part_name:argument_name}``: +You can change this behavior and define a mapping between route parameter and +an argument name with ``{route_parameter_name:controller_argument_name}``: .. configuration-block:: @@ -1019,11 +1019,11 @@ an argument name with ``{variable_part_name:argument_name}``: { // ... - #[Route('/blog/{slug:article}', name: 'blog_show')] - public function show(string $article): Response + #[Route('/blog/{slug:articleSlug}', name: 'blog_show')] + public function show(string $articleSlug): Response { - // $article will be equal to the dynamic part of the URL - // e.g. at /blog/yay-routing, then $article='yay-routing' + // $articleSlug will be equal to the dynamic part of the URL + // e.g. at /blog/yay-routing, then $articleSlug='yay-routing' // ... } @@ -1033,7 +1033,7 @@ an argument name with ``{variable_part_name:argument_name}``: # config/routes.yaml blog_show: - path: /blog/{slug:article} + path: /blog/{slug:articleSlug} controller: App\Controller\BlogController::show .. code-block:: xml @@ -1045,7 +1045,7 @@ an argument name with ``{variable_part_name:argument_name}``: xsi:schemaLocation="http://symfony.com/schema/routing https://symfony.com/schema/routing/routing-1.0.xsd"> - @@ -1056,7 +1056,7 @@ an argument name with ``{variable_part_name:argument_name}``: use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator; return function (RoutingConfigurator $routes): void { - $routes->add('blog_show', '/blog/{slug:article}') + $routes->add('blog_show', '/blog/{slug:articleSlug}') ->controller([BlogController::class, 'show']) ; }; @@ -1079,11 +1079,11 @@ an array: { // ... - #[Route('/blog/{id:article}/{slug:article}', name: 'blog_show')] - public function show(array $article): Response + #[Route('/blog/{id:articleData}/{slug:articleData}', name: 'blog_show')] + public function show(array $articleData): Response { - // $article will equal the dynamic part of the URL - // e.g. at /blog/12/yay-routing, then $article=['id' => '12', 'slug' => 'yay-routing'] + // $articleData will equal the dynamic part of the URL + // e.g. at /blog/12/yay-routing, then $articleData=['id' => '12', 'slug' => 'yay-routing'] // ... } @@ -1093,7 +1093,7 @@ an array: # config/routes.yaml blog_show: - path: /blog/{id:article}/{slug:article} + path: /blog/{id:articleData}/{slug:articleData} controller: App\Controller\BlogController::show .. code-block:: xml @@ -1105,7 +1105,7 @@ an array: xsi:schemaLocation="http://symfony.com/schema/routing https://symfony.com/schema/routing/routing-1.0.xsd"> - @@ -1116,7 +1116,7 @@ an array: use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator; return function (RoutingConfigurator $routes): void { - $routes->add('blog_show', '/blog/{id:article}/{slug:article}') + $routes->add('blog_show', '/blog/{id:articleData}/{slug:articleData}') ->controller([BlogController::class, 'show']) ; };