I am building a restful api in laravel 4 where there are users with different types of permission. I want to restrict access to different routes depending on the user role (which is saved in the user table in db)
How would I do that? Here is what I have so far (it's not working so far).
filters.php
//allows backend api access depending on the user's role once they are logged in
Route::filter('role', function()
{
return Auth::user()->role;
});
routes.php
Route::group(array('before' => 'role'), function($role) {
if($role==1){
Route::get('customer/retrieve/{id}', 'CustomerController@retrieve_single');
Route::post('customer/create', 'CustomerController@create');
Route::put('customer/update/{id}', 'CustomerController@update');
}
});
Is it possible that I'm writing the syntax wrong for a "group filter"?
Try the following:
filters.php
Route::filter('role', function()
{
if ( Auth::user()->role !==1) {
// do something
return Redirect::to('/');
}
});
routes.php
Route::group(array('before' => 'role'), function() {
Route::get('customer/retrieve/{id}', 'CustomerController@retrieve_single');
Route::post('customer/create', 'CustomerController@create');
Route::put('customer/update/{id}', 'CustomerController@update');
});