How do I register a middleware in Laravel 11?

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

Title: Simplifying Middleware Registration in Laravel 11: A Comprehensive Guide

Body: Laravel 11 introduced significant changes to the middleware system. The kernel.php file, which formerly handled its management, has been removed completely. This change may seem confusing at first for developers accustomed to previous versions of Laravel. However, registering custom middlewares in Laravel 11 is now a relatively straightforward process. In this post, we will discuss the new methods and best practices for managing your middleware in Laravel 11, as well as provide relevant code examples where needed.

The New Middleware System in Laravel 11

In Laravel 11, middlewares are now located within a App\Http\Middleware folder. This folder is created automatically when you run the 'make:auth' command to scaffold an authentication system for your application. Each of these middleware classes should extend the 'Illuminate\Http\Middleware\Middleware' class and have methods named according to Laravel conventions (e.g., handle()).

Registering Middlewares in Laravel 11

To register a custom middleware, you can perform two different steps depending on your application needs:

1. Registering the Middleware with Route Groups

If you need to apply your middleware only to specific routes or groups of routes, you should use the 'middleware' route option in Laravel. Here is a code example for this method:

php
Route::middleware(['auth', 'my‑custom-middleware'])->group(function () {
    // Add your protected routes here...
});

In this example, the 'auth' middleware ensures basic authentication, while the 'my-custom-middleware' is your custom-created middleware class. This method allows you to specify multiple middlewares at once.

2. Registering Middlewares Globally

If your middleware needs to be applied across your entire application or for all routes, you can register it globally using the 'middleware' option in your app's config/app.php file:

php
'providers' => [
    // ...
],

'aliases' => [
    // ...
],

'middleware' => [
    App\Http\Middleware\VerifyCsrfToken::class,
    App\Http\Middleware\CheckForMaintenanceMode::class,
    \Illuminate\Session\Middleware\StartSession::class,
    // Include your custom middleware here...
],

Here, you have added your custom middleware to the global list of middlewares for the application. Keep in mind that you should only add middlewares that need to be applied for every route and not just specific ones. This will also ensure proper performance, as Laravel won't need to do additional checks while executing requests with no applicable middlewares.

Including Middlewares from Vendor Packages

If you are using a vendor package that includes middleware classes, you can register them in your application by adding the corresponding file path to the 'middleware' array in config/app.php:

php
// App\Http\Kernel.php
<?php

namespace App\Http;

use Illuminate\Foundation\Application;
use Illuminate\Foundation\Request;
use Illuminate\Routing\Router;

class Kernel extends \Illuminate\Foundation\Http\Kernel {
    // ...
}
php
// config/app.php
<?php

return [
    'providers' => [
        // ...
    ],

    'aliases' => [
        // ...
    ],

    'middleware' => [
        \App\Http\Middleware\VerifyCsrfToken::class,
        App\Http\Middleware\CheckForMaintenanceMode::class,
        \Illuminate\Session\Middleware\StartSession::class,
        // Include your custom middleware here...
    ],
];

Conclusion:

Registering custom middlewares in Laravel 11 is a straightforward process, thanks to the removal of the kernel.php file. This allows for better organization and clarity when it comes to managing middlewares in your application. By following the methods outlined above, you can easily manage your middleware registrations depending on your needs and ensure proper security and performance across all routes.