目标:我想使用
在Laravel 4中进行路由过滤Route::group
和Route::filter
描述
我有两种类型的用户:
对于,Internal
,我有两组:
对于Distributor
,我有4个小组:
符合条件的路线
OEM经销商仅有5条路线。
Route::get('distributors/{id}', array('before' =>'profile', 'uses'=>'DistributorController@show'));
Route::get('distributors/{id}/edit', 'DistributorController@edit');
Route::put('distributors/{id}/update', array('as'=>'distributors.update', 'uses'=>'DistributorController@update'));
Route::get('catalog_downloads','CatalogDownloadController@index');
Route::get('catalog_downloads/{id}/download','CatalogDownloadController@file_download');
定期分销商有资格获得8条路线。
Route::get('distributors/{id}', array('before' =>'profile', 'uses'=>'DistributorController@show'));
Route::get('distributors/{id}/edit', 'DistributorController@edit');
Route::put('distributors/{id}/update', array('as'=>'distributors.update', 'uses'=>'DistributorController@update'));
Route::get('catalog_downloads','CatalogDownloadController@index');
Route::get('catalog_downloads/{id}/download','CatalogDownloadController@file_download');
Route::get('marketing_materials','MarketingMaterialController@index');
Route::get('marketing_materials/{id}/download/thumb_path','MarketingMaterialController@thumb_download');
Route::get('marketing_materials/{id}/download/media_path','MarketingMaterialController@media_download');
代码
问题
答案 0 :(得分:1)
您可以按照这样的路径
class UserController extends BaseController {
/**
* Instantiate a new UserController instance.
*/
public function __construct()
{
$this->beforeFilter('employee', array('only' => 'index'));
}
}
答案 1 :(得分:1)
首先关闭:无法宣布两次导致相同网址的路线。无论是否在团队中。 (如果你有一个prefix
的小组,那么因为前缀改变了路由的URL,这是可能的。
您必须通过智能过滤
解决此问题这是我提出的最简单的解决方案:
Route::filter('distributor', function(){
$user = Auth::user();
if($user->type == "Distributor"){
return true;
}
if (Request::ajax()){
return Response::make('Unauthorized', 404);
}
return View::make('errors.404_auth');
});
Route::filter('distributor.regular', function(){
$user = Auth::user();
if($user->type == "Distributor"){
if($user->distributor()->type != 'OEM'){
return true;
}
}
if (Request::ajax()){
return Response::make('Unauthorized', 404);
}
return View::make('errors.404_auth');
});
distributor
过滤器只检查用户是否为Distributor
类型。第二个过滤器distributor.regular
检查分销商是否不是OEM。 (如果你想知道,distributor.regular
中的点没有特殊功能或更深层含义。我只是喜欢这样写它)
Route::group(['before' => 'distributor'], function(){
Route::get('distributors/{id}', array('before' =>'profile', 'uses'=>'DistributorController@show'));
Route::get('distributors/{id}/edit', 'DistributorController@edit');
Route::put('distributors/{id}/update', array('as'=>'distributors.update', 'uses'=>'DistributorController@update'));
Route::get('catalog_downloads','CatalogDownloadController@index');
Route::get('catalog_downloads/{id}/download','CatalogDownloadController@file_download');
Route::group(['before' => 'distributor.regular'], function(){
Route::get('catalog_downloads', 'CatalogDownloadController@index');
Route::get('catalog_downloads/{id}/download', 'CatalogDownloadController@file_download');
Route::get('marketing_materials', 'MarketingMaterialController@index');
Route::get('marketing_materials/{id}/download/thumb_path', 'MarketingMaterialController@thumb_download');
Route::get('marketing_materials/{id}/download/media_path', 'MarketingMaterialController@media_download');
});
});
这应该已经适用于您发布的用例。但是,我们可以使过滤器更加灵活,同时减少冗余代码。
function makeError404(){
if (Request::ajax()){
return Response::make('Unauthorized', 404);
}
return View::make('errors.404_auth');
}
Route::filter('distributor', function(){
$user = Auth::user();
if($user->type == "Distributor"){
return true;
}
return makeError404();
});
Route::filter('distributor.group', function($route, $request, $value){
$groups = explode(';', $value);
$user = Auth::user();
if($user->type == "Distributor"){
if(in_array($user->distributor()->type, $groups)){
return true;
}
}
return makeError404();
});
现在我们可以动态指定用户必须在哪个组中...
Route::group(['before' => 'distributor'], function(){
// distributor routes
Route::group(['before' => 'distributor.group:gold;silver;bronze'], function(){
// regular routes
});
});