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

wordxpression's avatar

Question about the 'Kernel Replacement' in Laravel 11

Laravel 11 has no Kernel file in the app anymore, but we're supposed to configure Laravel 11 in the app/bootstrap/app.php. While installing Spatie MultiTenancy, there are a few lines I should have to add to the Kernel file... but I can't really figure out how to set the same in the app/bootstrap/app.php file.

Code to be added

protected $middlewareGroups = [
    'web' => [
        // ...
        \Spatie\Multitenancy\Http\Middleware\NeedsTenant::class,
        \Spatie\Multitenancy\Http\Middleware\EnsureValidTenantSession::class,
    ]
];

Since the app.php only calls a static method, I can't figure out how I could set protected vars from there.

Any hints?

0 likes
3 replies
LaryAI's avatar
Level 58

In Laravel 11, the kernel has been refactored, and the configuration of middleware groups has moved. Instead of modifying the $middlewareGroups property in the Kernel.php file, you now need to register your middleware in the app/bootstrap/app.php file using the MiddlewarePriority class.

Here's how you can add the Spatie MultiTenancy middleware to the 'web' middleware group in Laravel 11:

use Illuminate\Foundation\Application;
use Illuminate\Foundation\Http\Middleware\MiddlewarePriority;
use Spatie\Multitenancy\Http\Middleware\NeedsTenant;
use Spatie\Multitenancy\Http\Middleware\EnsureValidTenantSession;

$app = new Application(
    $_ENV['APP_BASE_PATH'] ?? dirname(__DIR__)
);

// ... other bootstrap code ...

// Register the Spatie MultiTenancy middleware in the 'web' group
MiddlewarePriority::forWeb([
    // ... other web middleware ...
    NeedsTenant::class,
    EnsureValidTenantSession::class,
]);

// ... rest of the bootstrap/app.php file ...

return $app;

Make sure to place the MiddlewarePriority::forWeb([...]) call at the appropriate place in your bootstrap/app.php file, where other middleware groups are being registered or after all other bootstrap code has been executed.

This should correctly register the Spatie MultiTenancy middleware in the 'web' middleware group, and you should be able to proceed with the installation of the Spatie MultiTenancy package as intended.

gych's avatar
gych
Best Answer
Level 29

You'll have to add it like this

->withMiddleware(function (Middleware $middleware) {
    $middleware->web(append: [
        \Spatie\Multitenancy\Http\Middleware\NeedsTenant::class,
        \Spatie\Multitenancy\Http\Middleware\EnsureValidTenantSession::class,
    ]);
})

Refer to this link for more information in the Laravel documentation on this topic https://laravel.com/docs/11.x/middleware#laravels-default-middleware-groups

7 likes

Please or to participate in this conversation.