phplaravelroutesservice-provider

Why Laravel routes registered in module custom service provider not available on url?


I'm refactoring backend part of existing Laravel 10 + Vue 3 SPA app to Modules that among other things contain route files that registered in module service provider:


class AuthenticationServiceProvider extends ServiceProvider
{
    public function boot(): void
    {
       $this->registerRoutes();
    }

    public function register()
    {
        // .....
    }

    protected function registerRoutes(): void
    {
        Route::middleware('api')
            ->prefix('api')
            ->namespace('App\\Modules\\Authentication\\Http\\Controllers\\')
            ->group(__DIR__ . '/../Authentication/routes/api/auth.php');
    }
}


This is auth.php file:

Route::group(['middleware' => ['api.auth'], 'prefix' => 'auth'], function () {
    Route::post('register', [AuthenticationController::class, 'register'])->name('auth.register')->withoutMiddleware('api.auth');
    Route::post('login', [AuthenticationController::class, 'login'])->name('auth.login')->withoutMiddleware('api.auth');
    Route::get('refresh', [AuthenticationController::class, 'refresh'])->name('auth.refresh')->withoutMiddleware('api.auth');
    Route::get('logout', [AuthenticationController::class, 'logout'])->name('auth.logout');
    Route::get('get-authenticated', [AuthenticationController::class, 'getAuthenticated'])->name('customers.get-authenticated');
});

Module service provider is registered in Laravel config app.php file in providers array:

This is my web.php:

Route::any('{all}', function () {
    return view('welcome');
})
    ->where('all', '^(?!api).*$')
    ->where('all', '^(?!storage).*$');

Routes are registered and visible in php artisan route:list, but when I try test this route I can't reach api/auth/login route and others

How to solve this problem?


Solution

  • Finally I've found solution - just place custom service providers before Laravel providers in config/app.php under Package services providers comment:

    ...
        'providers' => ServiceProvider::defaultProviders()->merge([
            /*
             * Package Service Providers...
             */
            \App\Modules\Authentication\AuthenticationServiceProvider::class,
            \App\Modules\Account\AccountServiceProvider::class,
    
    
            /*
             * Application Service Providers...
             */
            App\Providers\AppServiceProvider::class,
            App\Providers\AuthServiceProvider::class,
            // App\Providers\BroadcastServiceProvider::class,
            App\Providers\EventServiceProvider::class,
            App\Providers\RouteServiceProvider::class,
    
    
    
        ])->toArray(),
    ...