你是对的,对于每个传入的请求,Symfony 将根据URI、客户端的IP 地址、传入的来决定使用哪个access_control >“HOST NAME”!,以及请求方法。
但为了你的幸福,有一种方法可以不重复规则并实现相同的目标。
“特别parameters.yml文件”
它定义了通常在每个服务器上更改的值。
首先,在你的本地开发机器中,在app/config/parameters.yml文件中定义一个新参数,我们命名为“requires_channel”:
# in your local development machine (development)
parameters:
database_driver: pdo_mysql
# ...
requires_channel: http # <- Wow! I can use this var anywhere into my configuration :)
在生产服务器上做同样的事情,但设置https:
# in your deployment server (production)
# app/config/parameters.yml
parameters:
database_driver: pdo_mysql
# ...
requires_channel: https # <- I need enforce https redirection now in production server
现在,您可以为每个条目仅定义一个规则并使用新的 %requires_channel% 参数:
# app/config/security.yml
security:
# ...
access_control:
- { path: ^/admin, role: ROLE_ADMIN, requires_channel: '%requires_channel%' }
# ...
%requires_channel%:其值取决于运行应用程序的主机。
请注意,host 属性不再重要,因为之前需要区分这两个规则。
此解决方案可以通过使用环境配置文件(config_dev.yml 和 config_prod.yml)来完成,但您可能还需要在 localhost (http) 中测试您的应用程序的 prod 环境。所以上面的解决方案对你来说应该足够了;)
希望能帮到你!