8

I'm using SF2 and I created some routes helping the debugging of the project:

widget_debug_page:
    path:       /debug/widget/{widgetName}
    defaults:   { _controller: WidgetBundle:Debug:default }

The problem is that this route MUST never be reachable when going in production.

I could not find a parameter to specify the environment.

yivi
  • 42,438
  • 18
  • 116
  • 138
user2998227
  • 81
  • 1
  • 2

5 Answers5

26

Just for people who want use with annotation system:

/**
 * @Route("/my-route", condition="'dev' === '%kernel.environment%'")
 */
public function index() {}
Breith
  • 2,160
  • 1
  • 23
  • 32
  • Symfony ExpressionLanguage component is required https://symfony.com/doc/current/components/expression_language.html – nulll Feb 01 '23 at 11:51
10

You can add your route in routing_dev.yml (Symfony 2/3) or create a dev-only routing file in config/routes/dev/ (Symfony 4).

gronostaj
  • 2,231
  • 2
  • 23
  • 43
Jona
  • 1,156
  • 10
  • 16
  • 3
    Is it possible via annoations ? Maybe via the `condition` option (can't find documentation about it) – Leto Feb 09 '15 at 13:07
  • Doc for annotation: https://symfony.com/doc/current/routing/conditions.html – Breith Jun 22 '19 at 01:37
6

As mentioned perviously you can define the conditional route by adding 'condition' directive to the route definition. In case you need to limit the route for the appropriate environment you can check %kernel.environment% parameter value directly. For example:

widget_debug_page:
    path:       /debug/widget/{widgetName}
    defaults:   { _controller: WidgetBundle:Debug:default }
    condition:  "%kernel.environment% === 'dev'"
Tomas Liubinas
  • 181
  • 2
  • 7
3

I just had a similar problem myself since I wanted to create a "System temporarily down due to maintenance"-page that would disable my site entirely in prod-environment only as I use dev-environment to make sure everything is alright after deploying an update to my production server. I guess its debatable whether this is good practice, but anyway...

The comment made by Leto to a previous answer gave me an idea how to solve it and I think I have successfully managed to make my route only work in dev-environment like so:

default_update_view:
    path:     /{route}
    defaults: { _controller: WalanderBundle:Default:maintenanceInfo }
    condition: "request.getScriptName() == '/app.php'"
    requirements:
        route: .+

No the question was to only enable a route in dev-environment which would then be done by changing the condition as follows if I'm not missing anything obvious:

condition: "request.getScriptName() == '/app_dev.php'"

Hope this helps although the question was a bit old!

Perhaps in the case when you want to enable a route in the dev-environment only the already provided answer by JonaPkr is best practice though.

Community
  • 1
  • 1
walander
  • 55
  • 6
0

Symfony >= 5.3

when@dev:
    widget_debug_page:
    path:       /debug/widget/{widgetName}
    defaults:   { _controller: WidgetBundle:Debug:default }

reference: https://symfony.com/blog/new-in-symfony-5-3-configure-multiple-environments-in-a-single-file

Tobias Sette
  • 185
  • 12