Laravel: Where to declare middleware?

Routes versus Controllers… fight!

When it makes sense on Controllers?

The prefered way to set middleware is on the controllers if the actions require a middleware to fully work. There are three clear advantages of this approach:

  • A middleware affects all the controller actions, nothing more.
  • You can use only() and except() to selectively apply middleware on given routes.

When it makes sense on Routes

You should declare middleware on routes when the actions do not depend on the middleware.

Graphic Designer graduate. Full Stack Web Developer. Retired Tech & Gaming Editor.