Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

tobe81cwb's avatar

Multiple middleware on routing group

Trying to apply more than one middleware on a route group!

Currently, have several users, with several roles! Some users have access to a route only if is from a group.

for this, I have created a middleware (named auth.admin only for tests) The authentication that I'm using, is the AuthBasic.

So, I created a route group, with the following config:

Route::group(['namespace' => 'Admin', 'prefix' => 'admin', 'middleware' => 'auth.basic|auth.admin'], function() {
// foo
}

(example from http://laravel.com/docs/5.0/routing#route-groups)

the middleware auth.admin is registered on Kernel.php. When I try to access this route, Laravel throw a exception: Class auth.basic|auth.admin does not exist

if I try only auth.basic, works ok. If I try only auth.admin, works ok too.

I don't want to put this middleware on controller, because this route group have more than 10 controllers.

0 likes
9 replies
bobbybouwmann's avatar
Level 88

You need to pass an array to it I guess

Route::group(['namespace' => 'Admin', 'prefix' => 'admin', 'middleware' => ['auth.basic', 'auth.admin']], function() {});

If that doesn't work you have to split them I guess

Route::group(['middleware' => 'auth.basic'], function () {
    Route::group(['namespace' => 'Admin', 'prefix' => 'admin', 'middleware' => 'auth.admin'], function() {
        
    });
});


12 likes
bobbybouwmann's avatar

It is talking about an array though..

Middleware are applied to all routes within the group by defining the list of middleware with the middleware parameter on the group attribute array. Middleware will be executed in the order you define this array:

2 likes
pmall's avatar

I already used middleware with the pipe notation and it worked well. Strange.

kobbyknight's avatar

Hi, I also had this issue but i TRIED this out and it worked

Route::group(['middleware'=>['role:developer' OR 'role:admin']],function (){

//foo

});

2 likes
rhoyle's avatar

in setting up a group middleware in Laravel 8 with Jetstream installed, I used the middleware that was setup for the dashboard I just put it in to a group and IT WORKED !!

Route::group(['middleware' => 'auth:sanctum', 'verified'], function () { Route::get('/dashboard', function () { return view('dashboard'); })->name('dashboard'); Route::get('/users', function () { return view('admin.users.index'); })->name('admin.users'); });

Please or to participate in this conversation.