我有一个symfony2应用程序。
在prod服务器上,我希望所有路由都通过https,而在dev中,我希望能够使用http。我如何单独使用symfony2来实现这一点?我不想触摸Web服务器配置。
我试着在我的routing.yml
中添加这个
myBundle:
resource: "@MyBundle/Controller/"
type: annotation
prefix: /
schemes: [https]
而在我的routing_dev.yml
:中有这个
myBundle:
resource: "@MyBundle/Controller/"
type: annotation
prefix: /
schemes: [http]
_main:
resource: routing.yml
即使在开发模式下,它仍然想使用https。
您可以为此定义参数。在app/config/config.yml
中定义:
parameters:
httpProtocol: http
然后在app/config/config_prod.yml
:中
parameters:
httpProtocol: https
在routing.yml
中更改为:
myBundle:
resource: "@MyBundle/Controller/"
type: annotation
prefix: /
schemes: ['%httpProtocol%']
清除缓存(prod和dev),它应该可以工作了。
更改app.php的最后一行,如下所示:
if ($request->getScheme() === 'http') {
$urlRedirect = str_replace($request->getScheme(), 'https', $request->getUri());
$response = new RedirectResponse($urlRedirect);
} else {
$response = $kernel->handle($request);
}
$response->send();
$kernel->terminate($request, $response);
我的第一个解决方案运行良好,但应该注意不要在routing_dev.yml
中覆盖自己的路由。在文件的末尾,我有
_main:
resource: routing.yml
所以我所有的捆绑包路由都改回了https方案。对条目进行排序,使我的自定义条目最后出现,从而解决了问题。
我认为您可以检查app_dev.php
文件中的方案,并在此基础上将其重定向到https
$url = parse_url($_SERVER['HTTP_REFERER']);
if ($url['scheme'] == 'http') {
header("Location: https://{$_SERVER['SERVER_NAME']}{$_SERVER['PHP_SELF']}");
}
或者可以将.htaccess
中的一些更改应用于URL 中的app_dev.php