Symfony - Defining a route only in dev. environment

怎甘沉沦 提交于 2019-11-28 04:49:51

问题


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.


回答1:


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




回答2:


Just for people who want use with annotation system:

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



回答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.




回答4:


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


来源:https://stackoverflow.com/questions/28253545/symfony-defining-a-route-only-in-dev-environment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!