如何强制https为prod但http为dev环境?

k0p*_*kus 23 php https routing symfony

我有一个symfony2应用程序.

在prod服务器上,我希望我的所有路由都通过https,而在开发中我希望能够使用http.我如何单独使用symfony2实现这一目标?我不想触摸网络服务器配置.

我尝试在我的脑中添加这个 routing.yml

myBundle:
    resource: "@MyBundle/Controller/"
    type:     annotation
    prefix:   /
    schemes:  [https]
Run Code Online (Sandbox Code Playgroud)

在我的这个routing_dev.yml:

myBundle:
    resource: "@MyBundle/Controller/"
    type:     annotation
    prefix:   /
    schemes:  [http]

_main:
    resource: routing.yml
Run Code Online (Sandbox Code Playgroud)

它仍然想在开发模式下转到https.

paz*_*ulx 63

您可以为此定义参数.在app/config/config.yml定义:

parameters:
    httpProtocol: http
Run Code Online (Sandbox Code Playgroud)

然后在app/config/config_prod.yml:

parameters:
    httpProtocol: https
Run Code Online (Sandbox Code Playgroud)

routing.yml改为:

myBundle:
    resource: "@MyBundle/Controller/"
    type:     annotation
    prefix:   /
    schemes:  ['%httpProtocol%']
Run Code Online (Sandbox Code Playgroud)

清除缓存(prod和dev),它应该工作.

  • 对于任何想要在注释中使用这种情况的人来说,这就是这样的:@Route("/ register.html",schemes = {"%httpProtocol%"}) (5认同)
  • 那是个好主意!它也适用于配置`security.yml`. (4认同)