Laravel Controller Middleware

Controller Middleware

Middleware can be assigned to the controllers routes in our route files:

It is more convenient to specify middleware within our controllers constructor. Using the middleware method from our controllers constructor, we can easily assign middleware to the controllers action.

We even restrict the middleware to only certain methods on the controller class:

Controllers allow us to register middleware using a closure. It provides a convenient way to define a middleware for a single controller without defining an entire middleware class:

We assign middleware to a subset of controller actions; however, it indicates our controller is growing too large. Consider breaking our controller into multiple, smaller controllers.