Skip to content

Commit ad793ec

Browse files
committed
minor #21279 [Routing] correct environment-specific routing configuration examples (santysisi)
This PR was merged into the 6.4 branch. Discussion ---------- [Routing] correct environment-specific routing configuration examples ### Description This PR updates the Symfony routing documentation examples to reflect the correct way to define environment-specific routes using `when@dev` (YAML), `<when env="dev">` (XML), and conditional logic in PHP. ### Changes Made - **YAML**: Replaced `env: dev` with `when@dev` syntax block. - **XML**: Wrapped the `<route>` element in a `<when env="dev">` element. - **PHP**: Added conditional logic using `$routes->env()` to only register the route in the `dev` environment. ### Why? The previous examples used either unsupported keys or incorrect syntax for defining environment-specific routes (e.g. using env: dev directly inside a route definition). This update replaces those with the correct environment-based separation using when@dev blocks (YAML), <when env="dev"> wrappers (XML), and runtime conditionals in PHP. It ensures that environment-specific routes are properly scoped and configured according to Symfony's routing system. ### References * [Allowed keys in YAML routing](https://github.com/symfony/symfony/blob/6.4/src/Symfony/Component/Routing/Loader/YamlFileLoader.php#L36-L38) — the env key is not permitted inside route definitions. * [RoutingConfigurator::env() method](https://github.com/symfony/symfony/blob/6.4/src/Symfony/Component/Routing/Loader/Configurator/RoutingConfigurator.php#L63-L69) — this method returns the current environment and does not accept an environment name as an argument for filtering. * [RouteConfigurator class](https://github.com/symfony/symfony/blob/6.4/src/Symfony/Component/Routing/Loader/Configurator/RouteConfigurator.php#L19) — does not have an env() method. Commits ------- 5cac5ed [Routing] correct environment-specific routing configuration examples
2 parents c3543d5 + 5cac5ed commit ad793ec

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

routing.rst

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -282,10 +282,10 @@ given value:
282282
.. code-block:: yaml
283283
284284
# config/routes.yaml
285-
tools:
286-
path: /tools
287-
controller: App\Controller\DefaultController::developerTools
288-
env: dev
285+
when@dev:
286+
tools:
287+
path: /tools
288+
controller: App\Controller\DefaultController::developerTools
289289
290290
.. code-block:: xml
291291
@@ -296,9 +296,9 @@ given value:
296296
xsi:schemaLocation="http://symfony.com/schema/routing
297297
https://symfony.com/schema/routing/routing-1.0.xsd">
298298
299-
<route id="tools" path="/tools" controller="App\Controller\DefaultController::developerTools">
300-
<env>dev</env>
301-
</route>
299+
<when env="dev">
300+
<route id="tools" path="/tools" controller="App\Controller\DefaultController::developerTools"/>
301+
</when>
302302
</routes>
303303
304304
.. code-block:: php
@@ -308,10 +308,11 @@ given value:
308308
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
309309
310310
return function (RoutingConfigurator $routes): void {
311-
$routes->add('tools', '/tools')
312-
->controller([DefaultController::class, 'developerTools'])
313-
->env('dev')
314-
;
311+
if('dev' === $routes->env()) {
312+
$routes->add('tools', '/tools')
313+
->controller([DefaultController::class, 'developerTools'])
314+
;
315+
}
315316
};
316317
317318
.. _routing-matching-expressions:

0 commit comments

Comments
 (0)