要强制HTTPS在指定的路由上运行,Laravel文档要求执行以下操作:
Route::get('foo', array('https', function()
{
return 'Must be over HTTPS';
}));现在,在我的第一个Laravel应用程序中,我一直在使用资源控制器。我不认为我会把它们用于我的第二个应用程序,继续我已经读过的东西,但是现在他们很高兴地坐在我的router.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呢?
发布于 2014-01-07 12:13:23
Route::filter('forceHttps', function($req){
if (! Request::secure()) {
return Redirect::secure(Request::getRequestUri());
}
});
Route::group(['before' => 'forceHttps'], function(){
Route::resource('backoffice', 'BackofficeController');
});发布于 2014-01-07 13:07:15
假设您有一个像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('/', 'App\Controllers\BOindexController@index');
Route::resource('otherBackOfficeURI', 'App\Controllers\OtherBOController');
//other routes & controllers here...
});这样,从site.tld/backoffice开始的所有内容都将通过https过滤器(很可能是通过isAdmin过滤器),然后检查内部功能路由规则。我想这样会更方便。
https://stackoverflow.com/questions/20971135
复制相似问题