Back to Blog 2 min read

How to Configure Middleware In Laravel 11

Hi everyone, As you guys know Laravel 11 is officially released now. So, there are plenty of new things that has been released along with new structu...


Hi everyone, As you guys know Laravel 11 is officially released now. So, there are plenty of new things that has been released along with new structure.



If you look at the new structure, You won't see kernal.php the file in Laravel 11. So, you won't be able to configure middleware aliases in kernal.php.



Instead of this, you have got new structure bootstrap/app.php file to configure your middleware aliases in Laravel 11.






How you'll do that?



return Application::configure(basePath: dirname(__DIR__))
    ->withRouting(
        web: __DIR__ . '/../routes/web.php',
        commands: __DIR__ . '/../routes/console.php',
        health: '/up',
    )
    ->withMiddleware(function (Middleware $middleware) {
        $middleware->alias([
            'customerAccess' => CustomerAccessMiddleware::class,
            'adminAccess' => AdminAccessMiddleware::class
        ]);
    })
    ->withExceptions(function (Exceptions $exceptions) {
        //
    })->create()



See the below code.



 $middleware->alias([
            'customerAccess' => CustomerAccessMiddleware::class,
            'adminAccess' => AdminAccessMiddleware::class
        ]);



This is what you have write there in withMiddleware() method binder.



Extra



You'll see some other methods as well. Such as route binding you can do in the withRouting() method. The same applies to withExceptions method.



I hope this article will help. If you want to add something please comment below. I'll add in the article when I'll get notified.



Recommended: WhereAll and WhereAny Method in Laravel 10.47 update