Ben
Ben

Reputation: 3060

Don't apply a middleware to a specific route

I want a piece of middleware to apply to all routes in a group, except for one route. Can I specify on the route that the middleware should not be applied?

Upvotes: 0

Views: 409

Answers (1)

jedrzej.kurylo
jedrzej.kurylo

Reputation: 40909

Route groups are a means of applying the same configuration to all routes they contain. I understand you want to share most of the middleware configuration with all your routes except one. You can use nested route groups to make most of that config shared.

The following should do the trick for you:

Route::group(['middleware' => ['a', 'b', 'c']], function () {
    Route::get('route1_with_3_middlewares', 'MyController@test');
    Route::group(['middleware' => 'd'], function () {
        Route::get('route2_with_4_middlewares', 'MyController@test');
        Route::get('route3_with_4_middlewares', 'MyController@test');
        Route::get('route4_with_4_middlewares', 'MyController@test');
    });
});

This way you have middlewares a, b and c applied to all routes and middleware d applied to all routes except the first one.

Upvotes: 0

Related Questions