Goal: I want to make route filter in Laravel 4 using
Route::group
andRoute::filter
Description
I have 2 types of user :
- Internal
- Distributor
For, Internal
, I have 2 groups:
- admin
- regular
For Distributor
, I have 4 groups:
- gold
- silver
- bronze
- oem
Eligible Route
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');
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');
Code
Questions
- Can someone please help me or at least direct me to the right direction ?
As you ask, I show you another aproach to the problem. I think it's more flexible and maintainable.
My final target it's get that kind of routes:
So that you
acl.permitted
filter checks if the user is allowed to access to some route. Let's take a look to the filter.Now we can declare the filter. For example in the filter file:
Then we need the User->Group->Permission hierarchy.
Then you can seed the permissions table with all the route alias with that.
Finally you need to create your groups ('Internal' and 'Distributor' in your case) and atach them to the appropiate permissions. If you don't want to create the users-group-permissions maintenance then you can do it in a seed file.
The main advantaje of this solution is that you don't have to harcoded the permission's logic, and it's a more global solution. On the other hand you'd probably need to create the group-permissions maintenance admin page sometime in the future.
I hope it may help you.
According to your situation ...
I suggest:
Auth::user()->type
right in your routes.phpHere is the code - please modify to fit your exact needs.