Laravel 3 HTTP和HTTPS路由 [英] Laravel 3 HTTP and HTTPS routes
问题描述
我有以下代码:
Route::get('/', function()
{
return 'non secure page';
});
Route::get('/', array('https' => true, function()
{
return 'secure page';
}));
我希望发生的事情是,这两条路线将得到不同的对待.第一个用于 http://example.com 请求,第二个用于
What I expected to happen is that these two routes would be treated differently. The first is for http://example.com requests and the second for https://example.com. Respectively, these pages should show the text 'non secure page' and 'secure page'. What actually happens is that both show the text 'secure page'. This must mean that both routes are treated the same i.e. it doesn't matter if the request was over https or http - the same route is triggered.
我知道我可以使用if (Request::secure()){ //routes };
解决我的问题,但这又使我想到laravel中HTTPS安全路由有什么用?它们有什么作用?何时应使用它们?
I know I can resolve my issue by using if (Request::secure()){ //routes };
but that then leads me to the question what use are the HTTPS secure routes in laravel? What do they achieve and when should they be used?
我看了看文档,但是我不清楚应该发生什么.
I've looked at the docs, but it's not clear to me what is supposed to happen.
推荐答案
文档说:
在定义路由时,可以使用"https"属性来指示在生成URL或重定向到该路由时应使用HTTPS协议.
When defining routes, you may use the "https" attribute to indicate that the HTTPS protocol should be used when generating a URL or Redirect to that route.
"https"和::secure()
仅在生成路由的URL时使用,它们不用于提供仅https的路由.您可以编写一个过滤器来防止非HTTPS路由(下面的示例).或者,如果您想阻止对整个域的任何非HTTPS访问,则应该重新配置服务器,而不是在PHP中进行此操作.
"https" and ::secure()
are only used when generating URLs to routes, they're not used to provide https-only routes. You could write a filter to protect against non-HTTPS routes (example below). Or if you want to prevent any non-HTTPS access to your entire domain then you should reconfigure your server, rather than do this in PHP.
Route::filter('https', function() {
if (!Request::secure()) return Response::error(404);
});
替代过滤器响应:
Route::filter('https', function() {
if (!Request::secure()) return Redirect::to_secure(URI::current());
});
参考文献:
- http://laravel.com/docs/routing#https-routes
- http://laravel.com/docs/routing#filters
这篇关于Laravel 3 HTTP和HTTPS路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!