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

Ligonsker's avatar

How to handle middleware that are almost for all routes except for a few?

I wanted to move the auth Middleware to be global in Kernel.php, because right now it wraps around most of the routes.

But then there are a few routes that should not have it.

I just thought it was weird to have the auth Middleware wrapping most of the web.php file:

Route::group(['middlware']=> ['auth']], function () {
   // most of the routes in web.php
}

but in this case it looks like the best option. Or you would do it differently?

0 likes
1 reply
vybeauregard's avatar
Level 54

Because of the exceptions you mention, wrapping the majority of your routes in web.php with auth middleware is probably the best path forward.

If this is really bugging you, you could spend some time separating all of your auth routes into their own custom routes file (something like auth.php) but that would probably make it more confusing in the long run.

1 like

Please or to participate in this conversation.