phplaravellaravel-middleware

Unable to make my own 'api' middleware alias in order to use a dependency with laravel 11


So basically I work on a Laravel 11 project, and the session cookie is generated by a portal from which I access my application.

I have an AuthMiddleware aliased to 'sso' which is responsible for requesting to the portal app with the cookie and retrieve the user, and authenticate him.

I'm using a library in my vendors which exposes routes which are protected by a middleware aliased to 'api'.

My issue is that since I don't have any middleware aliased to 'api' in my application, my user is never authenticated and thus, I can't use the library for the moment.

I tried to replace my alias 'sso' to api like so :

->withRouting(
    web: __DIR__ . '/../routes/web.php',
    api: __DIR__ . '/../routes/api.php',
    commands: __DIR__ . '/../routes/console.php',
    health: '/up',
)
->withMiddleware(function (Middleware $middleware) {
    $middleware->alias([
        'api' => AuthMiddleware::class,
    ]);

but if I try to dd() something from within AuthMiddleware, this middleware is never called when associated to alias 'api'.

What's wrong ? is there a mismatch somewhere ?

Thanks.


Solution

  • I finally found the solution :

    ->withRouting(
        web: __DIR__ . '/../routes/web.php',
        api: __DIR__ . '/../routes/api.php',
        commands: __DIR__ . '/../routes/console.php',
        health: '/up',
        then: function () {
            Route::middleware('sso')
                ->group(base_path('vendor/path/to/routes/api.php'));
        }
    )
    

    My 'sso' middleware is not overriding the 'api' middleware mentionned in the dependency route file.