diff --git a/src/Illuminate/Foundation/Configuration/ApplicationBuilder.php b/src/Illuminate/Foundation/Configuration/ApplicationBuilder.php index 9ee99e97646a..21fcade21dde 100644 --- a/src/Illuminate/Foundation/Configuration/ApplicationBuilder.php +++ b/src/Illuminate/Foundation/Configuration/ApplicationBuilder.php @@ -130,7 +130,7 @@ public function withRouting(?Closure $using = null, AppRouteServiceProvider::loadRoutesUsing($using); $this->app->booting(function () { - $this->app->register(AppRouteServiceProvider::class); + $this->app->register(AppRouteServiceProvider::class, force: true); }); if (is_string($commands) && realpath($commands) !== false) { diff --git a/src/Illuminate/Foundation/Support/Providers/RouteServiceProvider.php b/src/Illuminate/Foundation/Support/Providers/RouteServiceProvider.php index 9c9ba4f90be6..323f0f32b42a 100644 --- a/src/Illuminate/Foundation/Support/Providers/RouteServiceProvider.php +++ b/src/Illuminate/Foundation/Support/Providers/RouteServiceProvider.php @@ -90,7 +90,7 @@ protected function routes(Closure $routesCallback) */ public static function loadRoutesUsing(Closure $routesCallback) { - static::$alwaysLoadRoutesUsing = $routesCallback; + self::$alwaysLoadRoutesUsing = $routesCallback; } /** @@ -134,9 +134,11 @@ protected function loadCachedRoutes() */ protected function loadRoutes() { - if (! is_null(static::$alwaysLoadRoutesUsing)) { - $this->app->call(static::$alwaysLoadRoutesUsing); - } elseif (! is_null($this->loadRoutesUsing)) { + if (! is_null(self::$alwaysLoadRoutesUsing)) { + $this->app->call(self::$alwaysLoadRoutesUsing); + } + + if (! is_null($this->loadRoutesUsing)) { $this->app->call($this->loadRoutesUsing); } elseif (method_exists($this, 'map')) { $this->app->call([$this, 'map']); diff --git a/tests/Integration/Foundation/Support/Providers/RouteServiceProviderTest.php b/tests/Integration/Foundation/Support/Providers/RouteServiceProviderTest.php new file mode 100644 index 000000000000..20005e6270dc --- /dev/null +++ b/tests/Integration/Foundation/Support/Providers/RouteServiceProviderTest.php @@ -0,0 +1,75 @@ +withProviders([ + AppRouteServiceProvider::class, + ]) + ->withRouting( + using: function () { + Route::get('login', fn () => 'Login')->name('login'); + } + ) + ->withMiddleware(function (Middleware $middleware) { + // + }) + ->withExceptions(function (Exceptions $exceptions) { + // + })->create(); + } + + public function test_it_can_register_multiple_route_service_providers() + { + Assert::assertArraySubset([ + RouteServiceProvider::class => true, + AppRouteServiceProvider::class => true, + ], $this->app->getLoadedProviders()); + } + + public function test_it_can_uses_routes_registered_using_bootstrap_file() + { + $this->get(route('login')) + ->assertOk() + ->assertSee('Login'); + } + + public function test_it_can_uses_routes_registered_using_configuration_file() + { + $this->get(route('dashboard')) + ->assertOk() + ->assertSee('Hello'); + } +} + +class AppRouteServiceProvider extends RouteServiceProvider +{ + /** + * Bootstrap any application services. + * + * @return void + */ + public function boot() + { + $this->routes(function () { + Route::get('dashboard', fn () => 'Hello')->name('dashboard'); + }); + } +}