Get "PHP 8 in a Nuthshell" (Now with PHP 8.4)
Amit Merchant

Amit Merchant

A blog on PHP, JavaScript, and more

Resolving middlewares statically from controllers in Laravel 9.x

In Laravel, it’s possible to resolve middleware from controllers. For example, you can resolve a middleware from the controller’s constructor like so.

public function __construct()
{
    $this->middleware('auth');
}

As you can tell, we can resolve a middleware from the controller’s constructor using the middleware() method. I have explained this in one of my previous articles. You can check that article out.

But the problem with this approach is that the middleware will be resolved every time the controller is instantiated. So, if you have a controller with 10 methods, the middleware will be resolved 10 times. Also, according to this issue, since the middleware is resolved every time the controller is instantiated before middlewares are applied, it doesn’t affect the dependencies that have been passed through the constructor.

To mitigate this problem, Laravel 9.x introduced a new way to resolve middlewares statically from controllers. So, if you’re using Laravel 9.x, you can resolve middlewares statically from controllers. Let’s see how it works with an example.

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Routing\Controllers\HasMiddleware;
use Illuminate\Routing\Controllers\Middleware;

class UserController extends Controller implements HasMiddleware
{
    public static function middleware()
    {
        return [
            (new Middleware('auth')),
        ];
    }

    public function index()
    {
        //
    }
}

As you can tell,

we can now resolve middlewares statically from controllers using the middleware() method. Here, we can return an array of middlewares that we want to resolve statically from the controller. To do so, we can use the Middleware class (a value object) which accepts the middleware name (or array of middlewares) as its only argument.

Also, we need to make sure that the controller implements the HasMiddleware interface. This is necessary to tell Laravel that the controller has middlewares that need to be resolved statically.

Now, if you want to resolve multiple middlewares from the controller, you can do so like so.

public static function middleware()
{
    return [
        (new Middleware('auth')),
        (new Middleware('role:admin')),
    ];
}

You can also resolve middlewares with only and except methods like so.

public static function middleware()
{
    return [
        (new Middleware('auth'))->only('index'),
        (new Middleware('role:admin'))->except('show'),
    ];
}

And that’s it! This is how you can resolve middlewares statically from controllers in Laravel 9.x. This also solves the issue people were facing with the previous approach of resolving middlewares from the controller’s constructor.

Learn the fundamentals of PHP 8 (including 8.1, 8.2, 8.3, and 8.4), the latest version of PHP, and how to use it today with my new book PHP 8 in a Nutshell. It's a no-fluff and easy-to-digest guide to the latest features and nitty-gritty details of PHP 8. So, if you're looking for a quick and easy way to PHP 8, this is the book for you.

Like this article?

Buy me a coffee

👋 Hi there! I'm Amit. I write articles about all things web development. You can become a sponsor on my blog to help me continue my writing journey and get your brand in front of thousands of eyes.

Comments?