@@ -323,12 +323,24 @@ arbitrary matching logic:
323
323
* )
324
324
*
325
325
* 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%'"
327
327
*/
328
328
public function contact(): Response
329
329
{
330
330
// ...
331
331
}
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
+ }
332
344
}
333
345
334
346
.. code-block :: php-attributes
@@ -353,6 +365,16 @@ arbitrary matching logic:
353
365
{
354
366
// ...
355
367
}
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
+ }
356
378
}
357
379
358
380
.. code-block :: yaml
@@ -367,6 +389,11 @@ arbitrary matching logic:
367
389
# expressions can even use environment variables:
368
390
# condition: "context.getHost() == env('APP_MAIN_HOST')"
369
391
392
+ post_show :
393
+ path : /posts/{id}
394
+ controller : ' App\Controller\DefaultController::showPost'
395
+ condition : " params['id'] < 1000"
396
+
370
397
.. code-block :: xml
371
398
372
399
<!-- config/routes.xml -->
@@ -383,6 +410,10 @@ arbitrary matching logic:
383
410
<!-- expressions can even use environment variables: -->
384
411
<!-- <condition>context.getHost() == env('APP_MAIN_HOST')</condition> -->
385
412
</route >
413
+
414
+ <route id =" post_show" path =" /posts/{id}" controller =" App\Controller\DefaultController::showPost" >
415
+ <condition >params['id'] < 1000</condition >
416
+ </route >
386
417
</routes >
387
418
388
419
.. code-block :: php
@@ -400,6 +431,10 @@ arbitrary matching logic:
400
431
// expressions can even use environment variables:
401
432
// ->condition('context.getHost() == env("APP_MAIN_HOST")')
402
433
;
434
+ $routes->add('post_show', '/posts/{id}')
435
+ ->controller([DefaultController::class, 'showPost'])
436
+ ->condition('params["id"] < 1000')
437
+ ;
403
438
};
404
439
405
440
The value of the ``condition `` option is any valid
@@ -414,6 +449,14 @@ and can use any of these variables created by Symfony:
414
449
The :ref: `Symfony Request <component-http-foundation-request >` object that
415
450
represents the current request.
416
451
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
+
417
460
You can also use this function:
418
461
419
462
``env(string $name) ``
@@ -478,6 +521,8 @@ controller action that you expect:
478
521
479
522
[OK] Route "app_lucky_number" matches
480
523
524
+ .. _routing-route-parameters :
525
+
481
526
Route Parameters
482
527
----------------
483
528
0 commit comments