roles-permissions-laravel icon indicating copy to clipboard operation
roles-permissions-laravel copied to clipboard

Add permission to new route and controller

Open rddewan opened this issue 7 years ago • 1 comments

Hi I just download and is working great . How can i add this permission to new custom method ?

i have a controller method readAllItems() insted of index() //my route Route::group( ['middleware' => ['auth']], function() { Route::resource('users', 'UserController'); Route::resource('roles', 'RoleController'); Route::resource('posts', 'PostController'); Route::resource('price', 'PriceController'); Route::resource('items', 'ItemController');

});

but when i visit http://localhost/items i get error method index in not found how can i make use of my own method instead of index , edit ,store,destroy

Thank you

rddewan avatar Sep 21 '17 04:09 rddewan

Why don't you create separate routes, don't use Route::resource('items', 'ItemController') just create route like this

Route::get('items', 'ItemController@readAllItems');
Route::get('items/{id}', 'ItemController@show');
Route::put('items/{id}', 'ItemController@update');
Route::delete('items/{id}', 'ItemController@destroy');

And now you wont be able to use Authorizable trait so to authorize an action you need to manually call $this->authorize('edit_item') in your controller method.

class ItemController extends Controller {
   public function readAllItems() {
       $this->authorize('view_item');
       ...
       // your logic
   }
}

I would suggest you stick to the naming convention if you can.

saqueib avatar Sep 22 '17 05:09 saqueib