Skip to content

Document route environment condition #21081

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

Open
wants to merge 1 commit into
base: 6.4
Choose a base branch
from
Open
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
66 changes: 66 additions & 0 deletions routing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,72 @@ Use the ``methods`` option to restrict the verbs each route should respond to:

.. _routing-matching-expressions:

Matching Environments
~~~~~~~~~~~~~~~~~~~~~

The ``env`` option can be used to make a route conditional on the
:ref:`configuration environment <configuration-environments>`, the route will
only be registered if the environment matches.

.. configuration-block::

.. code-block:: php-attributes

// src/Controller/DefaultController.php
namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;

class DefaultController extends AbstractController
{
#[Route(
'/tools',
name: 'tools',
env: 'dev',
)]
public function developerTools(): Response
{
// ...
}
}

.. code-block:: yaml

# config/routes.yaml
tools:
path: /tools
controller: App\Controller\DefaultController::developerTools
env: dev

.. code-block:: xml

<!-- config/routes.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<routes xmlns="http://symfony.com/schema/routing"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/routing
https://symfony.com/schema/routing/routing-1.0.xsd">

<route id="tools" path="/tools" controller="App\Controller\DefaultController::developerTools">
<env>dev</env>
</route>
</routes>

.. code-block:: php

// config/routes.php
use App\Controller\DefaultController;
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;

return function (RoutingConfigurator $routes): void {
$routes->add('tools', '/tools')
->controller([DefaultController::class, 'developerTools'])
->env('dev')
;
};

Matching Expressions
~~~~~~~~~~~~~~~~~~~~

Expand Down