File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,68 @@ Use the ``methods`` option to restrict the verbs each route should respond to:
247247
automatically for you when the :ref:`framework.http_method_override <configuration-framework-http_method_override>`
248248
option is ``true``.
249249

250+
Matching Environments
251+
~~~~~~~~~~~~~~~~~~~~~
252+
253+
Use the ``env`` option to register a route only when the current
254+
:ref:`configuration environment <configuration-environments>` matches the
255+
given value:
256+
257+
.. configuration-block::
258+
259+
.. code-block:: php-attributes
260+
261+
// src/Controller/DefaultController.php
262+
namespace App\Controller;
263+
264+
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
265+
use Symfony\Component\HttpFoundation\Response;
266+
use Symfony\Component\Routing\Attribute\Route;
267+
268+
class DefaultController extends AbstractController
269+
{
270+
#[Route('/tools', name: 'tools', env: 'dev')]
271+
public function developerTools(): Response
272+
{
273+
// ...
274+
}
275+
}
276+
277+
.. code-block:: yaml
278+
279+
# config/routes.yaml
280+
tools:
281+
path: /tools
282+
controller: App\Controller\DefaultController::developerTools
283+
env: dev
284+
285+
.. code-block:: xml
286+
287+
<!-- config/routes.xml -->
288+
<?xml version="1.0" encoding="UTF-8" ?>
289+
<routes xmlns="http://symfony.com/schema/routing"
290+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
291+
xsi:schemaLocation="http://symfony.com/schema/routing
292+
https://symfony.com/schema/routing/routing-1.0.xsd">
293+
294+
<route id="tools" path="/tools" controller="App\Controller\DefaultController::developerTools">
295+
<env>dev</env>
296+
</route>
297+
</routes>
298+
299+
.. code-block:: php
300+
301+
// config/routes.php
302+
use App\Controller\DefaultController;
303+
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
304+
305+
return function (RoutingConfigurator $routes): void {
306+
$routes->add('tools', '/tools')
307+
->controller([DefaultController::class, 'developerTools'])
308+
->env('dev')
309+
;
310+
};
311+
250312
.. _routing-matching-expressions:
251313

252314
Matching Expressions

0 commit comments

Comments
 (0)