From b4efe17d6f1acc8fd6071003e823d966696d8551 Mon Sep 17 00:00:00 2001 From: HypeMC Date: Sat, 23 Apr 2022 08:40:12 +0200 Subject: [PATCH] [Routing] Add params variable to condition expression --- routing.rst | 60 ++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 55 insertions(+), 5 deletions(-) diff --git a/routing.rst b/routing.rst index a4028c4dc96..7aa8c3d30ce 100644 --- a/routing.rst +++ b/routing.rst @@ -323,12 +323,25 @@ arbitrary matching logic: * ) * * expressions can also include configuration parameters: - * condition: "request.headers.get('User-Agent') matches '%app.allowed_browsers%'" + * condition="request.headers.get('User-Agent') matches '%app.allowed_browsers%'" */ public function contact(): Response { // ... } + + /** + * expressions can retrieve route parameter values using the "params" variable + * @Route( + * "/posts/{id}", + * name="post_show", + * condition="params['id'] < 1000" + * ) + */ + public function showPost(int $id): Response + { + // ... return a JSON response with the post + } } .. code-block:: php-attributes @@ -346,13 +359,24 @@ arbitrary matching logic: '/contact', name: 'contact', condition: "context.getMethod() in ['GET', 'HEAD'] and request.headers.get('User-Agent') matches '/firefox/i'", + // expressions can also include config parameters: + // condition: "request.headers.get('User-Agent') matches '%app.allowed_browsers%'" )] - // expressions can also include config parameters: - // condition: "request.headers.get('User-Agent') matches '%app.allowed_browsers%'" public function contact(): Response { // ... } + + #[Route( + '/posts/{id}', + name: 'post_show', + // expressions can retrieve route parameter values using the "params" variable + condition: "params['id'] < 1000" + )] + public function showPost(int $id): Response + { + // ... return a JSON response with the post + } } .. code-block:: yaml @@ -367,6 +391,12 @@ arbitrary matching logic: # expressions can even use environment variables: # condition: "context.getHost() == env('APP_MAIN_HOST')" + post_show: + path: /posts/{id} + controller: 'App\Controller\DefaultController::showPost' + # expressions can retrieve route parameter values using the "params" variable + condition: "params['id'] < 1000" + .. code-block:: xml @@ -383,6 +413,11 @@ arbitrary matching logic: + + + + params['id'] < 1000 + .. code-block:: php @@ -400,6 +435,11 @@ arbitrary matching logic: // expressions can even use environment variables: // ->condition('context.getHost() == env("APP_MAIN_HOST")') ; + $routes->add('post_show', '/posts/{id}') + ->controller([DefaultController::class, 'showPost']) + // expressions can retrieve route parameter values using the "params" variable + ->condition('params["id"] < 1000') + ; }; The value of the ``condition`` option is any valid @@ -414,6 +454,14 @@ and can use any of these variables created by Symfony: The :ref:`Symfony Request ` object that represents the current request. +``params`` + An array of matched :ref:`route parameters ` for + the current route. + +.. versionadded:: 6.1 + + The ``params`` variable was introduced in Symfony 6.1. + You can also use this function: ``env(string $name)`` @@ -478,6 +526,8 @@ controller action that you expect: [OK] Route "app_lucky_number" matches +.. _routing-route-parameters: + Route Parameters ---------------- @@ -1385,7 +1435,7 @@ A possible solution is to change the parameter requirements to be more permissiv // src/Controller/DefaultController.php namespace App\Controller; - + use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; @@ -1505,7 +1555,7 @@ when importing the routes. // src/Controller/BlogController.php namespace App\Controller; - + use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route;