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.
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! 🚀
Please or to participate in this conversation.