tag
507
1
Laravel

Override group middleware on a per-route basis?

Posted 3 years ago by tag

Say I have the following route setup:

Route::group([
    'middleware' => ['one', 'two']
    'prefix' => '/some/uri',
    'namespace' => '/Some/Namespace'
], function() {
    
    Route::get('/whatever', [
        'middleware' => 'three'
        'uses' => 'SomeController@action'
    ]);

        // .... more routes

});

What ends up happening for the route is it that it now has one, two, and three middlewares - this is fine as there are a few situations where you do want your middlewares to merge, but other times you want the route middleware to override the group middleware rather than merge with it.

Is there a way to tell Laravel to do this without re-defining the middleware in the controller and setting 'only' to an empty array? I didn't even see a forgetMiddleware() method in controllers which would be better, but still not great.

Please sign in or create an account to participate in this conversation.