Title: Middleware based on laravel route parameter
Tags: php;laravel;laravel-7
Question: I have a route like this:
Rout75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0get('users/{type}', ['as'=&gt;'users.index', 'uses'=&gt;'UserController@index', 'middleware' =&gt; ['can:user-index']])-&gt;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
Gat75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0before(function ($user, $ability) {
$permission = Permission75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0where('slug', $ability)-&gt;first();
return $user-&gt;hasPermissionTo($permission);
});
Laravel Version: 7.9.2
PHP Version: 7.2

Here is the accepted answer: No you can't do dynamic middleware but you don't have to do it in controller too. You may do something like that; You bind two middleware to that route.
Rout75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0get('users/{type}', ['as'=&gt;'users.index', 'uses'=&gt;'UserController@index', 'middleware' =&gt; ['can:members-index', 'can:enquiries-index']])-&gt;where('type', '(members|enquiries)');
Just check type is as expected in that middleware, such as;
// can:enquiries-index middleware class
public function handle($request, Closure $next)
if ($request-&gt;route()-&gt;parameter('type') === 'enquiries') {
// enquiries related code blocks
// can:members-index middleware class
public function handle($request, Closure $next)
if ($request-&gt;route()-&gt;parameter('type') === 'members') {
// members related code blocks
While using policies, it is possible to use same check block inside the service provider such request()-&gt;route()-&gt;parameter('type').
Comment for this answer: Why not make a single one and check inside it?
Comment for this answer: @Ersoy Can I have access to route parameters in Providers. Because I'm using Gate definition for authorizing the routes. Check the updated question.
Comment for this answer: @Ersoy Yeah, I am able to access route parameter through request().
Comment for this answer: @Ersoy can you update the answer according to my code so that I can accept the answer..
Comment for this answer: separation of concerns @vivek_23
Comment for this answer: @vivek_23 but when you wrote i noticed a mistake - this one is easily expendable without modifying none of the classes.
Comment for this answer: @SunnyKumar you may try to access via using `request()->route()->parameter('type')` inside your ServiceProvider - request could be resolved.


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2020:00:05/14/20', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<php><laravel><laravel-7>', 'Title': 'Middleware based on laravel route parameter', 'ViewCount': 391, 'id': 61802104, 'user_AboutMe': '<p>An IT geek with hard willingness of learning and sharing new skills.</p>\n', 'user_AccountId': 9130416.0, 'user_DisplayName': 'Sunny Kumar', 'user_Reputation': 524.0, 'user_Views': 121.0, 'user_id': 6792282.0}}