How to Configure Middleware In Laravel 11

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 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

The post How to Configure Middleware In Laravel 11 appeared first on Larachamp.