Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

poptech's avatar
Level 20

How to Add Middleware Aliases in Laravel 11

Hello Laravel enthusiasts,

With the exciting changes in Laravel 11, we now work with Bootstrap/App instead of Http/Kernel. This shift allows for more streamlined configurations. A handy feature I discovered is how to add $middlewareAliases. Here’s a quick guide:

In Laravel 11, you can easily add middleware aliases by modifying the Application::configure() method. This method now handles most of what you did in Http/Kernel previously. Here's a snippet showing how to add middleware aliases:

return Application::configure()
    ->withProviders()
    ->withRouting(
        web: __DIR__.'/../routes/web.php',
        // api: __DIR__.'/../routes/api.php',
        commands: __DIR__.'/../routes/console.php',
        // channels: __DIR__.'/../routes/channels.php',
    )
    ->withMiddleware(function (Middleware $middleware) {
        $middleware->alias(['role' => RoleMiddleware::class]);
    })
    ->withExceptions(function (Exceptions $exceptions) {
        //
    })->create();

This approach simplifies adding middleware aliases and aligns with Laravel 11's new configuration style. It's a great example of Laravel's evolving, developer-friendly architecture.

Hope this helps anyone looking to adapt to the new Laravel 11 structure!

Happy coding! 🚀

0 likes
4 replies
davidvandertuijn's avatar

Thanks for this example!, now we know what a middleware alias is, but imagine you are a beginner and you now have no examples at all in a Laravel 11 project.

4 likes
hamza_bashir's avatar

@poptech can we separate middlewares for the API and web like in previous versions as we were able to write API and web base separately

Please or to participate in this conversation.