如何在Laravel 4中对路由组应用多个过滤器? [英] How to apply multiple filters on route group in Laravel 4?

查看:79
本文介绍了如何在Laravel 4中对路由组应用多个过滤器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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


说明

我有2种类型的用户:

  1. 内部
  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 ?
        • 推荐答案

          根据您的情况...

          我建议:

          1. 直接在 routes.php
          2. 中检查您的Auth::user()->type
          3. 不要在检查用户类型条件之前忘记检查Auth :: check()
          4. 为OEM执行此操作,重复为非OEM执行相同的逻辑.
          1. check your Auth::user()->type right in your routes.php
          2. Don't forget to check Auth::check() before checking the user type condition
          3. Do it for OEM and repeat the same logic for non OEM.

          这是代码-请进行修改以满足您的确切需求.

          Here is the code - please modify to fit your exact needs.

          <?
          
          // OEM Routes
          if(Auth::check()){
              if ( (Auth::user()->type == "Distributor") AND (Auth::user()->distributor()->first()->type == 'OEM') ){
          
                  Route::group(array('before'=>'auth'),function() {
                      Route::group(array('before'=>'csrf'),   function(){ 
          
                      // Other important routes like sign-out, dashboard, or change password should also listed here
                      Route::get('/account/sign-out',array('as'=>'account-sign-out','uses'=>'AccountController@getSignOut' ));
                      Route::get('/dashboard', array('as' =>'dashboard','uses'=>'HomeController@dashboard'));
          
                      // Allow routes
                      Route::get('distributors/{id}', array('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');
          
          
                  }); 
              }
          }else{
              return Redirect::route('home'); // I assume you have this declare somewhere
          }
          
          
          
          // Not OEM Routes  
          if(Auth::check()){
              if ( (Auth::user()->type == "Distributor") AND (Auth::user()->distributor()->first()->type !== 'OEM') ){
          
                  Route::group(array('before'=>'auth'),function() {
                      Route::group(array('before'=>'csrf'),   function(){ 
          
                      // Other important routes like sign-out, dashboard, or change password should also listed here
                      Route::get('/account/sign-out',array('as'=>'account-sign-out','uses'=>'AccountController@getSignOut' ));
                      Route::get('/dashboard', array('as' =>'dashboard','uses'=>'HomeController@dashboard'));
          
          
                      // Allow routes 
                      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');
          
                      Route::get('distributors/{id}', array('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');
          
          
                  }); 
              }
          }else{
              return Redirect::route('home'); // I assume you have this declare somewhere
          }
          

          这篇关于如何在Laravel 4中对路由组应用多个过滤器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆