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

[Routing] Add params variable to condition expression#16747

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
wouterj merged 1 commit intosymfony:6.1fromHypeMC:route-params-in-conditions
May 1, 2022
Merged
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 55 additions & 5 deletionsrouting.rst
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -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
Expand All@@ -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
Expand All@@ -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

<!-- config/routes.xml -->
Expand All@@ -383,6 +413,11 @@ arbitrary matching logic:
<!-- expressions can even use environment variables: -->
<!-- <condition>context.getHost() == env('APP_MAIN_HOST')</condition> -->
</route>

<route id="post_show" path="/posts/{id}" controller="App\Controller\DefaultController::showPost">
<!-- expressions can retrieve route parameter values using the "params" variable -->
<condition>params['id'] &lt; 1000</condition>
</route>
</routes>

.. code-block:: php
Expand All@@ -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
Expand All@@ -414,6 +454,14 @@ and can use any of these variables created by Symfony:
The :ref:`Symfony Request <component-http-foundation-request>` object that
represents the current request.

``params``
An array of matched :ref:`route parameters <routing-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)``
Expand DownExpand Up@@ -478,6 +526,8 @@ controller action that you expect:

[OK] Route "app_lucky_number" matches

.. _routing-route-parameters:

Route Parameters
----------------

Expand DownExpand Up@@ -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;
Expand DownExpand Up@@ -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;
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp