Laravel 3 HTTP和HTTPS路由

Gaz*_*dge 2 laravel laravel-3

我有以下代码:

Route::get('/',   function()
{
    return 'non secure page';
});
Route::get('/',  array('https' => true, function()
{
    return 'secure page';
}));
Run Code Online (Sandbox Code Playgroud)

我期望发生的是这两条路线将被区别对待.第一个是http://example.com请求,第二个是https://example.com.这些页面应分别显示"非安全页面"和"安全页面".实际发生的是,两者都显示文本"安全页面".这必须意味着两条路由的处理方式相同,即请求是否超过https或http无关紧要 - 触发相同的路由.

我知道我可以通过使用来解决我的问题,if (Request::secure()){ //routes };但这会引导我提出在laravel中使用HTTPS安全路由的问题是什么?他们取得了什么,何时使用?

我看过文档,但我不清楚应该发生什么.

Phi*_*rks 8

文件说:

定义路由时,您可以使用"https"属性指示在生成URL或重定向到该路由时应使用HTTPS协议.

"https" ::secure()仅用于生成路由的URL时,它们不用于提供仅https路由.您可以编写一个过滤器来防止非HTTPS路由(例如下面的例子).或者,如果您想阻止对整个域的任何非HTTPS访问,那么您应该重新配置服务器,而不是在PHP中执行此操作.

Route::filter('https', function() {
    if (!Request::secure()) return Response::error(404);
});
Run Code Online (Sandbox Code Playgroud)

替代过滤器响应:

Route::filter('https', function() {
    if (!Request::secure()) return Redirect::to_secure(URI::current());
});
Run Code Online (Sandbox Code Playgroud)

参考文献:

  1. http://laravel.com/docs/routing#https-routes
  2. http://laravel.com/docs/routing#filters