@@ -323,12 +323,25 @@ 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
+ * expressions can retrieve route parameter values using the "params" variable
335
+ * @Route(
336
+ * "/posts/{id}",
337
+ * name="post_show",
338
+ * condition="params['id'] < 1000"
339
+ * )
340
+ */
341
+ public function showPost(int $id): Response
342
+ {
343
+ // ... return a JSON response with the post
344
+ }
332
345
}
333
346
334
347
.. code-block :: php-attributes
@@ -346,13 +359,24 @@ arbitrary matching logic:
346
359
'/contact',
347
360
name: 'contact',
348
361
condition: "context.getMethod() in ['GET', 'HEAD'] and request.headers.get('User-Agent') matches '/firefox/i'",
362
+ // expressions can also include config parameters:
363
+ // condition: "request.headers.get('User-Agent') matches '%app.allowed_browsers%'"
349
364
)]
350
- // expressions can also include config parameters:
351
- // condition: "request.headers.get('User-Agent') matches '%app.allowed_browsers%'"
352
365
public function contact(): Response
353
366
{
354
367
// ...
355
368
}
369
+
370
+ #[Route(
371
+ '/posts/{id}',
372
+ name: 'post_show',
373
+ // expressions can retrieve route parameter values using the "params" variable
374
+ condition: "params['id'] < 1000"
375
+ )]
376
+ public function showPost(int $id): Response
377
+ {
378
+ // ... return a JSON response with the post
379
+ }
356
380
}
357
381
358
382
.. code-block :: yaml
@@ -367,6 +391,12 @@ arbitrary matching logic:
367
391
# expressions can even use environment variables:
368
392
# condition: "context.getHost() == env('APP_MAIN_HOST')"
369
393
394
+ post_show :
395
+ path : /posts/{id}
396
+ controller : ' App\Controller\DefaultController::showPost'
397
+ # expressions can retrieve route parameter values using the "params" variable
398
+ condition : " params['id'] < 1000"
399
+
370
400
.. code-block :: xml
371
401
372
402
<!-- config/routes.xml -->
@@ -383,6 +413,11 @@ arbitrary matching logic:
383
413
<!-- expressions can even use environment variables: -->
384
414
<!-- <condition>context.getHost() == env('APP_MAIN_HOST')</condition> -->
385
415
</route >
416
+
417
+ <route id =" post_show" path =" /posts/{id}" controller =" App\Controller\DefaultController::showPost" >
418
+ <!-- expressions can retrieve route parameter values using the "params" variable -->
419
+ <condition >params['id'] < 1000</condition >
420
+ </route >
386
421
</routes >
387
422
388
423
.. code-block :: php
@@ -400,6 +435,11 @@ arbitrary matching logic:
400
435
// expressions can even use environment variables:
401
436
// ->condition('context.getHost() == env("APP_MAIN_HOST")')
402
437
;
438
+ $routes->add('post_show', '/posts/{id}')
439
+ ->controller([DefaultController::class, 'showPost'])
440
+ // expressions can retrieve route parameter values using the "params" variable
441
+ ->condition('params["id"] < 1000')
442
+ ;
403
443
};
404
444
405
445
The value of the ``condition `` option is any valid
@@ -414,6 +454,14 @@ and can use any of these variables created by Symfony:
414
454
The :ref: `Symfony Request <component-http-foundation-request >` object that
415
455
represents the current request.
416
456
457
+ ``params ``
458
+ An array of matched :ref: `route parameters <routing-route-parameters >` for
459
+ the current route.
460
+
461
+ .. versionadded :: 6.1
462
+
463
+ The ``params `` variable was introduced in Symfony 6.1.
464
+
417
465
You can also use this function:
418
466
419
467
``env(string $name) ``
@@ -478,6 +526,8 @@ controller action that you expect:
478
526
479
527
[OK] Route "app_lucky_number" matches
480
528
529
+ .. _routing-route-parameters :
530
+
481
531
Route Parameters
482
532
----------------
483
533
@@ -1385,7 +1435,7 @@ A possible solution is to change the parameter requirements to be more permissiv
1385
1435
1386
1436
// src/Controller/DefaultController.php
1387
1437
namespace App\Controller;
1388
-
1438
+
1389
1439
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
1390
1440
use Symfony\Component\HttpFoundation\Response;
1391
1441
use Symfony\Component\Routing\Annotation\Route;
@@ -1505,7 +1555,7 @@ when importing the routes.
1505
1555
1506
1556
// src/Controller/BlogController.php
1507
1557
namespace App\Controller;
1508
-
1558
+
1509
1559
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
1510
1560
use Symfony\Component\HttpFoundation\Response;
1511
1561
use Symfony\Component\Routing\Annotation\Route;
0 commit comments