I need to provide access to particular users for specific role types and I created the gate below.
AuthServiceProvider.php
Gate::define('member', function ($user) {
if($user->role === 'Member')
{
return true;
}
return false;
});
So my question is, how can I use this gate in my router to say - 'these routes are only allowed for these users'? I know how to do that in the Controller, but was hoping there was an easier way to accomplish this before even getting to the controller.
Something like, only members can access these routes (maybe theres a way to group routes by user type?):
Route::get('/shop', function () {
return view('/shop');
})->middleware('can:member');
Route::get('/profile', function () {
return view('/profile');
})->middleware('can:member');
Note, I don't have any policies set up, as I don't think I need them and mostly looking to restrict access in certain pages and was hoping to just use gates for this, is this okay or is it bad practice?
I appreciate any advice on how to do this.
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire