Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[11.x] Allow RouteServiceProvider to be loaded more than once #49732

Merged
merged 4 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ protected function routes(Closure $routesCallback)
*/
public static function loadRoutesUsing(Closure $routesCallback)
{
static::$alwaysLoadRoutesUsing = $routesCallback;
self::$alwaysLoadRoutesUsing = $routesCallback;
}

/**
Expand Down Expand Up @@ -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']);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace Illuminate\Tests\Integration\Foundation\Support\Providers;

use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Foundation\Configuration\Middleware;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider;
use Illuminate\Support\Facades\Route;
use Illuminate\Testing\Assert;
use Orchestra\Testbench\TestCase;

class RouteServiceProviderTest extends TestCase
{
/**
* Resolve application implementation.
*
* @return \Illuminate\Foundation\Application
*/
protected function resolveApplication()
{
return Application::configure(static::applicationBasePath())
->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');
});
}
}