Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit22da5a1

Browse files
committed
feature#16747 [Routing] Add params variable to condition expression (HypeMC)
This PR was merged into the 6.1 branch.Discussion----------[Routing] Add params variable to condition expressionResolves#16736Commits-------b4efe17 [Routing] Add params variable to condition expression
2 parents6be527e +b4efe17 commit22da5a1

File tree

1 file changed

+55
-5
lines changed

1 file changed

+55
-5
lines changed

‎routing.rst‎

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -323,12 +323,25 @@ 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+
* 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+
}
332345
}
333346
334347
..code-block::php-attributes
@@ -346,13 +359,24 @@ arbitrary matching logic:
346359
'/contact',
347360
name: 'contact',
348361
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%'"
349364
)]
350-
// expressions can also include config parameters:
351-
// condition: "request.headers.get('User-Agent') matches '%app.allowed_browsers%'"
352365
public function contact(): Response
353366
{
354367
// ...
355368
}
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+
}
356380
}
357381
358382
..code-block::yaml
@@ -367,6 +391,12 @@ arbitrary matching logic:
367391
# expressions can even use environment variables:
368392
# condition: "context.getHost() == env('APP_MAIN_HOST')"
369393
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+
370400
..code-block::xml
371401
372402
<!-- config/routes.xml-->
@@ -383,6 +413,11 @@ arbitrary matching logic:
383413
<!-- expressions can even use environment variables:-->
384414
<!-- <condition>context.getHost() == env('APP_MAIN_HOST')</condition>-->
385415
</route>
416+
417+
<routeid="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']&lt; 1000</condition>
420+
</route>
386421
</routes>
387422
388423
..code-block::php
@@ -400,6 +435,11 @@ arbitrary matching logic:
400435
// expressions can even use environment variables:
401436
// ->condition('context.getHost() == env("APP_MAIN_HOST")')
402437
;
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+
;
403443
};
404444
405445
The value of the ``condition`` option is any valid
@@ -414,6 +454,14 @@ and can use any of these variables created by Symfony:
414454
The:ref:`Symfony Request<component-http-foundation-request>` object that
415455
represents the current request.
416456

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+
417465
You can also use this function:
418466

419467
``env(string $name)``
@@ -478,6 +526,8 @@ controller action that you expect:
478526
479527
[OK] Route "app_lucky_number" matches
480528
529+
.. _routing-route-parameters:
530+
481531
Route Parameters
482532
----------------
483533

@@ -1385,7 +1435,7 @@ A possible solution is to change the parameter requirements to be more permissiv
13851435
13861436
// src/Controller/DefaultController.php
13871437
namespace App\Controller;
1388-
1438+
13891439
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
13901440
use Symfony\Component\HttpFoundation\Response;
13911441
use Symfony\Component\Routing\Annotation\Route;
@@ -1505,7 +1555,7 @@ when importing the routes.
15051555
15061556
// src/Controller/BlogController.php
15071557
namespace App\Controller;
1508-
1558+
15091559
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
15101560
use Symfony\Component\HttpFoundation\Response;
15111561
use Symfony\Component\Routing\Annotation\Route;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp