8000 [Routing] Add params variable to condition expression by HypeMC · Pull Request #16747 · symfony/symfony-docs · GitHub
[go: up one dir, main page]

Skip to content
8000

[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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 1, 2022
Merged
Changes from all 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 deletions routing.rst
Original file line number Diff line number Diff 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 Down Expand Up @@ -478,6 +526,8 @@ controller action that you expect:

[OK] Route "app_lucky_number" matches

.. _routing-route-parameters:

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

Expand Down Expand 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 Down Expand 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
0