How to apply multiple filters on route group in Laravel 4?

前端 未结 2 1278
终归单人心
终归单人心 2021-01-17 01:33

Goal: I want to make route filter in Laravel 4 using Route::group and Route::filter


2条回答
  •  粉色の甜心
    2021-01-17 01:45

    According to your situation ...

    I suggest:

    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.

    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
    }
    

提交回复
热议问题