DEV Community

jsfreu
jsfreu

Posted on

Middleware registration in bootstrap/app.php Laravel 11

I've recently encountered a significant challenge while working with Laravel 11. In this version, kernel.php has been moved away, and the process for middleware registration has shifted to app.php. This change has resulted in an error that I'm struggling to resolve. Here’s the error message I’m facing:
`Fatal error: Uncaught Error: Call to undefined method Illuminate\Foundation\Configuration\Middleware::configure() in C:\laragon\www\project\bootstrap\app.php:15
Stack trace:

0 C:\laragon\www\project\vendor\laravel\framework\src\Illuminate\Foundation\Configuration\ApplicationBuilder.php(250): {closure}(Object(Illuminate\Foundation\Configuration\Middleware))

1 C:\laragon\www\project\vendor\laravel\framework\src\Illuminate\Container\Container.php(1294): Illuminate\Foundation\Configuration\ApplicationBuilder->Illuminate\Foundation\Configuration{closure}(Object(Illuminate\Foundation\Http\Kernel), Object(Illuminate\Foundation\Application))

2 C:\laragon\www\project\vendor\laravel\framework\src\Illuminate\Container\Container.php(1258): Illuminate\Container\Container->fireCallbackArray(Object(Illuminate\Foundation\Http\Kernel), Array)

3 C:\laragon\www\project\vendor\laravel\framework\src\Illuminate\Container\Container.php(1244): Illuminate\Container\Container->fireAfterResolvingCallbacks('Illuminate\Cont...', Object(Illuminate\Foundation\Http\Kernel))

4 C:\laragon\www\project\vendor\laravel\framework\src\Illuminate\Container\Container.php(805): Illuminate\Container\Container->fireResolvingCallbacks('Illuminate\Cont...', Object(Illuminate\Foundation\Http\Kernel))

5 C:\laragon\www\project\vendor\laravel\framework\src\Illuminate\Foundation\Application.php(1041): Illuminate\Container\Container->resolve('Illuminate\Cont...', Array, true)

6 C:\laragon\www\project\vendor\laravel\framework\src\Illuminate\Container\Container.php(723): Illuminate\Foundation\Application->resolve('Illuminate\Cont...', Array)

7 C:\laragon\www\project\vendor\laravel\framework\src\Illuminate\Foundation\Application.php(1026): Illuminate\Container\Container->make('Illuminate\Cont...', Array)

8 C:\laragon\www\project\vendor\laravel\framework\src\Illuminate\Foundation\Application.php(1181): Illuminate\Foundation\Application->make('Illuminate\Cont...')

9 C:\laragon\www\project\public\index.php(17): Illuminate\Foundation\Application->handleRequest(Object(Illuminate\Http\Request))

10 {main} thrown in C:\laragon\www\project\bootstrap\app.php on line 15

`

My current app.php looks like this:

<?php

use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Foundation\Configuration\Middleware;

return Application::configure(basePath: dirname(__DIR__))
    ->withRouting(
        web: __DIR__.'/../routes/web.php',
        commands: __DIR__.'/../routes/console.php',
        health: '/up',
    )
    ->withMiddleware(function (Middleware $middleware) {
        // Define middleware aliases directly as an array
        $middleware->configure([
            'aliases' => [
                'auth' => \App\Http\Middleware\Authenticate::class,
                'adminAuth' => \App\Http\Middleware\AdminAuth::class,
                'superAdminAuth' => \App\Http\Middleware\SuperAdminAuth::class,
            ],
            // You can add more middleware configurations here if necessary
        ]);
    })
    ->withExceptions(function (Exceptions $exceptions) {
        // Exceptions configuration logic here
    })->create();

Enter fullscreen mode Exit fullscreen mode

I’m encountering a Fatal error: Uncaught Error: Call to undefined method Illuminate\Foundation\Configuration\Middleware::configure(). This issue seems to be related to the way middleware is registered in Laravel 11, which is different from previous versions.

If anyone has experience with this new setup in Laravel 11, I would greatly appreciate some guidance on how to properly configure middleware in app.php. Thank you in advance for your help!

Top comments (2)

Collapse
 
shahghasiadil profile image
Shahghasi Adil • Edited

<?php

use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Foundation\Configuration\Middleware;

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([
'auth' => \App\Http\Middleware\Authenticate::class,
'adminAuth' => \App\Http\Middleware\AdminAuth::class,
'superAdminAuth' => \App\Http\Middleware\SuperAdminAuth::class,
]);
})
->withExceptions(function (Exceptions $exceptions) {
// Exceptions configuration logic here
})->create();

this will work

Collapse
 
jsfreu profile image
jsfreu

Hi thank you for this piece of code correction it trowed me a error, because DIR wasn't defined. Here is the corrected working code.

<?php

define('DIR', DIR);

use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Foundation\Configuration\Middleware;

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([
'auth' => \App\Http\Middleware\Authenticate::class,
'adminAuth' => \App\Http\Middleware\AdminAuth::class,
'superAdminAuth' => \App\Http\Middleware\SuperAdminAuth::class,
]);
})
->withExceptions(function (Exceptions $exceptions) {
// Exceptions configuration logic here
})->create();