skip to Main Content

Laravel 11 does not come with a middleware file and the kernel.php file has been removed altogther. So, when I create a custom middleware, how do I register it?

I do not know where to register middleware. Laravel 11 has been very confusing.

2

Answers


  1. in kernel.php file
    insert middleware class name

    Login or Signup to reply.
  2. In laravel 11 you can not register in the kernel.php the middlewares anymore. However there is plenty of other way how you can register the middlewares.

    If you want it to be triggered in every call you can append it to the bootstrap/app.php .

    ->withMiddleware(function (Middleware $middleware) {
         $middleware->append(YourMiddleware::class);
    })
    

    Otherwise you can add middlewares to specific route or route groups:

    Route::get('/yourroute', function () {
        
    })->middleware(YourMiddleware::class);
    

    If you add a middleware to a route group but you dont want certain routes to trigger the middleware, you can use the withoutMiddleware method

    Route::middleware([YourMiddleware::class])->group(function () {
        Route::get('/', function () {
            // ...
        });
     
        Route::get('/yourroute', function () {
            // ...
        })->withoutMiddleware([YourMiddleware::class]);
    });
    

    For more information check out the official documentation: https://laravel.com/docs/11.x/middleware#registering-middleware

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search