Laravel: Where to declare middleware?

Routes versus Controllers… fight!

Photo by Waldemar Brandt on Unsplash

While checking code from a coworker about a Laravel app, I saw that controllers constructors declared middleware, while routes also declared middleware. While this is normal on a Laravel application, some middleware were redundant.

After asking why, I received a weird answer: “I didn’t know which was was more appropriate, so I went for both”.

So, let’s have a clear purpose on why you should declare middleware on controllers, and when to use the routes.

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:

  • It makes your routes declaration cleaner and easy to maintain.
  • A middleware affects all the controller actions, nothing more.
  • You can use only() and except() to selectively apply middleware on given routes.

For example, we should use our custom admin middleware for a controller that handles banning management, like baning an user from the site. This middleware checks that only admins can enter to these actions.

On the other hand, if we have a middleware that changes the color of the site

When it makes sense on Routes

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

For example, we have a middleware called colorize, which applies a CSS style based on the post category. If we don’t declare it, all post will look the same, but doesn’t break the site in any way.

Not only we can assign it to only one route, but group multiple routes with the same middleware.

Let’s say, we want to apply the colorize middleware to the posts and the comments section.

You may want to prioritize grouping multiple routes into a middleware to avoid repeating the same middleware over and over again in dozens of routes.

And that’s pretty much the reasoning behind it. Selectively using middleware for each route it can become very cumbersome over time, specially if you handle more than dozen or you plan to move routes.

Grouping routes with a common middleware can be a very good tool, even if you don’t prefix the grouped routes.

--

--

--

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

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Quickfix: The AdditionalProtocArguments parameter is not supported by the ProtoCompile task

How to use Pooled Task Feature in UBIAI

MicroStrategy World 2022 is coming!

Rpy2 is turning 13

Coworkers Will Become Customers — It’s a Good thing

Zero Knowledge Proof — Deep into zkEVM source code (EVM Circuit)

Insiders Secret To Cracking the Google Summer Of Code — Part 2

A problem well stated is a problem half solved

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Italo Baeza Cabrera

Italo Baeza Cabrera

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

More from Medium

Running Laravel Out Of A Subdirectory

New features were added in Laravel 8.74.0

Features of Laravel Framework

What’s New in Laravel 9: New Features of Laravel 9