I have a route like this:
Route::get('users/{type}', ['as'=>'users.index', 'uses'=>'UserController@index', 'middleware' => ['can:user-index']])->where('type', '(members|enquiries)');
Can I use dynamic middleware here based on route parameter type.
Like If type is enquiries then middleware should be 'can:enquiries-index', similarly for members 'can:members-index'.
I know this can be done in controller function with authorize method, but I am using all authorizations in route file only. SO wanted to keep this here only.
I am using Laravel Gates definition for authorizing routes:
In AuthServiceProvider@boot
Gate::before(function ($user, $ability) {
$permission = Permission::where('slug', $ability)->first();
return $user->hasPermissionTo($permission);
});
Laravel Version: 7.9.2
PHP Version: 7.2