在 Laravel 4 中使用 https 访问 route::resource



要强制HTTPS在命名路由上,Laravel文档说执行以下操作:

Route::get('foo', array('https', function()
{
    return 'Must be over HTTPS';
}));

现在,在我的第一个Laravel应用程序上,我一直在使用资源控制器。我不认为我会在我的第二个应用程序中使用它们,继续我从那以后阅读的内容,但现在它们愉快地坐在我的路由器.php文件中。

我想强制我的应用程序的后台部分使用 HTTPS。所以,我的开场策略如下:

Route::resource('backoffice', array('https','BackofficeController'));

拉维尔不喜欢这个阵列。

所以,相反,我想我会尝试输入下一个参数:

Route::resource('backoffice', 'BackofficeController', 'https'));

但是下一个参数需要是一个数组。我找不到这方面的文档,但我将其转换为数组。它仍然没有用。

Route::resource('backoffice', 'BackofficeController', array('https')));

我什至尝试过:

Route::resource('backoffice', 'BackofficeController', array('https'=>true)));

然而,这也失败了。那么,如何强制资源使用 https?

Route::filter('forceHttps', function($req){
    if (! Request::secure()) {
        return Redirect::secure(Request::getRequestUri());
    }
});
Route::group(['before' => 'forceHttps'], function(){
    Route::resource('backoffice', 'BackofficeController');
});

假设你有一个像Andreyco建议的过滤器函数,这似乎很好,你可以做类似的事情:

//Andreyco's filter
Route::filter('forceHttps', function($req){
if (! Request::secure()) {
    return Redirect::secure(Request::getRequestUri());
}
});
//backoffice group routing
Route::group(array('prefix' => 'backoffice', 'before' => 'forceHttps'), function()
{
    Route::any('/',                'AppControllersBOindexController@index');
    Route::resource('otherBackOfficeURI', 'AppControllersOtherBOController');
    //other routes & controllers here...
});

这样,以site.tld/backoffice开头的所有内容都将通过https过滤器(很可能通过isAdmin过滤器),然后检查内部函数路由规则。我认为这会更方便。

最新更新