php - How to make a route filters base on user type in Laravel 4? -
goal: want make route filter in laravel 4 using
route::group
,route::filter
description
i have 2 types of user :
- internal
- distributor
for, internal
, have 2 groups:
- admin
- regular
for distributor
, have 4 groups:
- gold
- silver
- bronze
- oem
eligible route
oem distributor eligible 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');
regular distributor eligible 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');
code
questions
- can please me or @ least direct me right direction ?
first off: it's not possibble declare route results in same url twice. whether it's in group or not. (well if have group prefix
it's possible because prefix changes url of route)
you have solve problem intelligent filtering
this simplest solution i've come 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'); });
the distributor
filter checks if user of type distributor
. second filter, distributor.regular
, checks if distributor not oem. (if you're wondering, dot in distributor.regular
has no special function or deeper meaning. write 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 work use-cases posted. can make filters more flexible , 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 can dynamically specify in group user has be...
route::group(['before' => 'distributor'], function(){ // distributor routes route::group(['before' => 'distributor.group:gold;silver;bronze'], function(){ // regular routes }); });
Comments
Post a Comment