如何在Laravel 4中基于用户类型制作路由过滤器? [英] How to make a route filters base on user type in Laravel 4?
问题描述
目标:我想使用
Route::group
和Route::filter
说明
我有2种类型的用户:
- 内部
- 发行人
对于Internal
,我有2个小组:
- 管理员
- 常规
对于Distributor
,我有4个小组:
- 黄金
- 银
- 青铜
- oem
符合条件的路线
OEM分销商仅可使用5条路线.
OEM Distributor are eligible for only 5 routes.
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条路线.
Regular Distributor are eligible for 8 routes.
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');
代码
- filters.php
- routes.php .
- filters.php
- routes.php.
- 有人可以帮助我,或者至少将我引导到正确的方向吗?
- Can someone please help me or at least direct me to the right direction ?
问题
推荐答案
首先:声明两次输入相同URL 的路由是不可能的.无论是否在一个小组中. (如果您有一个prefix
组,则可能是因为前缀更改为路由的URL)
First off: It's not possibble to declare a route that results in the same URL twice. Whether it's in a group or not. (Well if you have a group with prefix
it's possible because a prefix changes to URL of the route)
您必须通过智能过滤
这是我想出的最简单的解决方案:
This is the simplest solution I've come up with:
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
中的点没有特殊功能或更深层的含义.我只想这样写)
The distributor
filter checks just if the user is of type Distributor
. The second filter, distributor.regular
, checks if the distributor is not an OEM. (If you're wondering, the dot in distributor.regular
has no special function or deeper meaning. I just like to write it like that)
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');
});
});
这应该已经适用于您发布的用例.但是,我们可以使过滤器更灵活,并减少冗余代码.
This should already work with the use-cases you posted. However we can make the filters more flexible and also reduce redundant code.
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();
});
现在,我们可以动态指定用户必须属于哪个组...
Now we can dynamically specify in which group the user has to be...
Route::group(['before' => 'distributor'], function(){
// distributor routes
Route::group(['before' => 'distributor.group:gold;silver;bronze'], function(){
// regular routes
});
});
这篇关于如何在Laravel 4中基于用户类型制作路由过滤器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!