7
votes

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.

4

4 Answers

19
votes

Just for people who want use with annotation system:

/**
 * @Route("/my-route", condition="'dev' === '%kernel.environment%'")
 */
public function index() {}
9
votes

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).

5
votes

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'"
3
votes

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.