8000 [Routing] Add params variable to condition expression · symfony/symfony-docs@344bd7e · GitHub
[go: up one dir, main page]

Skip to content

Commit 344bd7e

Browse files
committed
[Routing] Add params variable to condition expression
1 parent b94166f commit 344bd7e

File tree

1 file changed

+46
-1
lines changed

1 file changed

+46
-1
lines changed

routing.rst

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,12 +323,24 @@ arbitrary matching logic:
323323
* )
324324
*
325325
* expressions can also include configuration parameters:
326-
* condition: "request.headers.get('User-Agent') matches '%app.allowed_browsers%'"
326+
* condition="request.headers.get('User-Agent') matches '%app.allowed_browsers%'"
327327
*/
328328
public function contact(): Response
329329
{
330330
// ...
331331
}
332+
333+
/**
334+
* @Route(
335+
* "/posts/{id}",
336+
* name="post_show",
337+
* condition="params['id'] < 1000"
338+
* )
339+
*/
340+
public function showPost(int $id): Response
341+
{
342+
// ... return a JSON response with the post
343+
}
332344
}
333345
334346
.. code-block:: php-attributes
@@ -353,6 +365,16 @@ arbitrary matching logic:
353365
{
354366
// ...
355367
}
368+
369+
#[Route(
370+
'/posts/{id}',
371+
name: 'post_show',
372+
condition: "params['id'] < 1000"
373+
)]
374+
public function showPost(int $id): Response
375+
{
376+
// ... return a JSON response with the post
377+
}
356378
}
357379
358380
.. code-block:: yaml
@@ -367,6 +389,11 @@ arbitrary matching logic:
367389
# expressions can even use environment variables:
368390
# condition: "context.getHost() == env('APP_MAIN_HOST')"
369391
392+
post_show:
393+
path: /posts/{id}
394+
controller: 'App\Controller\DefaultController::showPost'
395+
condition: "params['id'] < 1000"
396+
370397
.. code-block:: xml
371398
372399
<!-- config/routes.xml -->
@@ -383,6 +410,10 @@ arbitrary matching logic:
383410
<!-- expressions can even use environment variables: -->
384411
<!-- <condition>context.getHost() == env('APP_MAIN_HOST')</condition> -->
385412
</route>
413+
414+
<route id="post_show" path="/posts/{id}" controller="App\Controller\DefaultController::showPost">
415+
<condition>params['id'] &lt; 1000</condition>
416+
</route>
386417
</routes>
387418
388419
.. code-block:: php
@@ -400,6 +431,10 @@ arbitrary matching logic:
400431
// expressions can even use environment variables:
401432
// ->condition('context.getHost() == env("APP_MAIN_HOST")')
402433
;
434+
$routes->add('post_show', '/posts/{id}')
435+
->controller([DefaultController::class, 'showPost'])
436+
->condition('params["id"] < 1000')
437+
;
403438
};
404439
405440
The value of the ``condition`` option is any valid
@@ -414,6 +449,14 @@ and can use any of these variables created by Symfony:
414449
The :ref:`Symfony Request <component-http-foundation-request>` object that
415450
represents the current request.
416451

452+
``params``
453+
An array of matched :ref:`route parameters <routing-route-parameters>` for
454+
the current route.
455+
456+
.. versionadded:: 6.1
457+
458+
The ``params`` variable was introduced in Symfony 6.1.
459+
417460
You can also use this function:
418461

419462
``env(string $name)``
@@ -478,6 +521,8 @@ controller action that you expect:
478521
479522
[OK] Route "app_lucky_number" matches
480523
524+
.. _routing-route-parameters:
525+
481526
Route Parameters
482527
----------------
483528

0 commit comments

Comments
 (0)
0