使用 App::before
您也许可以利用app/filters.php 文件中的App::before() 块。
更改块以包含一个简单的检查以查看当前请求是否安全,如果不安全,则将其重定向。
App::before(function($request)
{
if( ! Request::secure())
{
return Redirect::secure(Request::path());
}
});
使用过滤器
另一种选择可能是像这样创建一个过滤器。人们通常也将其存储在app/filters.php。
Route::filter('force.ssl', function()
{
if( ! Request::secure())
{
return Redirect::secure(Request::path());
}
});
然后,您可以对任何路由、路由组或控制器强制执行该新过滤器。
个别路线
Route::get('something', ['before' => 'force.ssl'], function()
{
return "This will be forced SSL";
});
路由组
Route::group(['before' => 'force.ssl'], function()
{
// Routes here.
});
控制器
您需要在控制器的 __construct() 方法中执行此操作。
public function __construct()
{
$this->beforeFilter('force.ssl');
}