如何根据Laravel 4中的用户类型制作路由过滤器?

时间:2014-11-27 14:43:05

标签: php laravel laravel-4 routes

  

目标:我想使用Route::groupRoute::filter

在Laravel 4中进行路由过滤

  

描述

我有两种类型的用户:

  1. 内部
  2. 经销商
  3. 对于,Internal,我有两组:

    • 管理员
    • 常规

    对于Distributor,我有4个小组:

    • gold
    • 青铜
    • OEM
      

    符合条件的路线

    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');
    
      

    代码

      

    问题

    • 有人可以帮助我或至少指导我正确的方向

2 个答案:

答案 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
    });
});