Skip to content

Commit 5ce08d4

Browse files
authored
[13.x] Register subdomain routes before routes that are not linked to a domain (#55921)
* wip * wip * wip * linting
1 parent 7c9e306 commit 5ce08d4

File tree

3 files changed

+75
-3
lines changed

3 files changed

+75
-3
lines changed

src/Illuminate/Routing/RouteCollection.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,22 @@ protected function addToCollections($route)
6262
$domainAndUri = $route->getDomain().$route->uri();
6363

6464
foreach ($methods as $method) {
65-
$this->routes[$method][$domainAndUri] = $route;
65+
if ($route->getDomain()) {
66+
$domainRoutes = array_filter($this->routes[$method] ?? [], fn ($route) => $route->getDomain() !== null);
67+
68+
$this->routes[$method] = $domainRoutes + [$domainAndUri => $route] + ($this->routes[$method] ?? []);
69+
} else {
70+
$this->routes[$method][$domainAndUri] = $route;
71+
}
6672
}
6773

68-
$this->allRoutes[implode('|', $methods).$domainAndUri] = $route;
74+
if ($route->getDomain()) {
75+
$domainRoutes = array_filter($this->allRoutes, fn ($route) => $route->getDomain() !== null);
76+
77+
$this->allRoutes = $domainRoutes + [implode('|', $methods).$domainAndUri => $route] + $this->allRoutes;
78+
} else {
79+
$this->allRoutes[implode('|', $methods).$domainAndUri] = $route;
80+
}
6981
}
7082

7183
/**

tests/Routing/RouteCollectionTest.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,4 +359,64 @@ public function testOverlappingRoutesMatchesFirstRoute()
359359
$this->assertCount(2, $this->routeCollection->getRoutes());
360360
$this->assertEquals('first', $this->routeCollection->match($request)->getName());
361361
}
362+
363+
public function testPrependsRoutesWithDomain()
364+
{
365+
$this->routeCollection->add(
366+
$noDomainGet1 = new Route('GET', 'no-domain-get1', ['uses' => 'NoDomainController@index'])
367+
);
368+
369+
$this->routeCollection->add(
370+
$fooDomainGet1 = (new Route('GET', 'foo-domain-get1', ['uses' => 'FooDomainController@index']))->domain('foo.test')
371+
);
372+
373+
$this->routeCollection->add(
374+
$barDomainGet1 = (new Route('GET', 'bar-domain-get1', ['uses' => 'BarDomainController@index']))->domain('bar.test')
375+
);
376+
377+
$this->routeCollection->add(
378+
$noDomainGet2 = new Route('GET', 'no-domain-get2', ['uses' => 'NoDomainController@show'])
379+
);
380+
381+
$this->routeCollection->add(
382+
$fooDomainGet2 = (new Route('GET', 'foo-domain-get2', ['uses' => 'FooDomainController@show']))->domain('foo.test')
383+
);
384+
385+
$this->routeCollection->add(
386+
$barDomainGet2 = (new Route('GET', 'bar-domain-get2', ['uses' => 'BarDomainController@show']))->domain('bar.test')
387+
);
388+
389+
$this->assertSame([
390+
$fooDomainGet1,
391+
$barDomainGet1,
392+
$fooDomainGet2,
393+
$barDomainGet2,
394+
$noDomainGet1,
395+
$noDomainGet2,
396+
], $this->routeCollection->getRoutes());
397+
398+
$this->assertSame([
399+
'GET' => [
400+
'foo.testfoo-domain-get1' => $fooDomainGet1,
401+
'bar.testbar-domain-get1' => $barDomainGet1,
402+
403+
'foo.testfoo-domain-get2' => $fooDomainGet2,
404+
'bar.testbar-domain-get2' => $barDomainGet2,
405+
406+
'no-domain-get1' => $noDomainGet1,
407+
'no-domain-get2' => $noDomainGet2,
408+
],
409+
410+
'HEAD' => [
411+
'foo.testfoo-domain-get1' => $fooDomainGet1,
412+
'bar.testbar-domain-get1' => $barDomainGet1,
413+
414+
'foo.testfoo-domain-get2' => $fooDomainGet2,
415+
'bar.testbar-domain-get2' => $barDomainGet2,
416+
417+
'no-domain-get1' => $noDomainGet1,
418+
'no-domain-get2' => $noDomainGet2,
419+
],
420+
], $this->routeCollection->getRoutesByMethod());
421+
}
362422
}

tests/Testing/Console/RouteListCommandTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ public function testDisplayRoutesForCli()
6363
$this->artisan(RouteListCommand::class)
6464
->assertSuccessful()
6565
->expectsOutput('')
66-
->expectsOutput(' GET|HEAD / ..................................................... ')
6766
->expectsOutput(' GET|HEAD {account}.example.com/ ................................ ')
67+
->expectsOutput(' GET|HEAD / ..................................................... ')
6868
->expectsOutput(' GET|HEAD closure ............................................... ')
6969
->expectsOutput(' POST controller-invokable Illuminate\Tests\Testing\Console\…')
7070
->expectsOutput(' GET|HEAD controller-method/{user} Illuminate\Tests\Testing\Cons…')

0 commit comments

Comments
 (0)