Skip to content

Commit a7fe439

Browse files
authored
Merge pull request #5961 from kenjis/fix-route-subdomain
fix: route limit to subdomains does not work
2 parents 1807ede + fbc94c1 commit a7fe439

2 files changed

Lines changed: 19 additions & 5 deletions

File tree

system/Router/RouteCollection.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1297,7 +1297,7 @@ private function getMethodParams(string $from): string
12971297
* Compares the subdomain(s) passed in against the current subdomain
12981298
* on this page request.
12991299
*
1300-
* @param mixed $subdomains
1300+
* @param string|string[] $subdomains
13011301
*/
13021302
private function checkSubdomains($subdomains): bool
13031303
{
@@ -1330,7 +1330,7 @@ private function checkSubdomains($subdomains): bool
13301330
* It's especially not perfect since it's possible to register a domain
13311331
* with a period (.) as part of the domain name.
13321332
*
1333-
* @return mixed
1333+
* @return false|string the subdomain
13341334
*/
13351335
private function determineCurrentSubdomain()
13361336
{
@@ -1351,7 +1351,7 @@ private function determineCurrentSubdomain()
13511351
}
13521352

13531353
// Get rid of any domains, which will be the last
1354-
unset($host[count($host)]);
1354+
unset($host[count($host) - 1]);
13551355

13561356
// Account for .co.uk, .co.nz, etc. domains
13571357
if (end($host) === 'co') {

tests/system/Router/RouteCollectionTest.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1128,7 +1128,7 @@ public function testWithDotCoSubdomain()
11281128
{
11291129
$routes = $this->getCollector();
11301130

1131-
$_SERVER['HTTP_HOST'] = 'example.uk.co';
1131+
$_SERVER['HTTP_HOST'] = 'example.co.uk';
11321132

11331133
$routes->add('/objects/(:alphanum)', 'Admin::objectsList/$1', ['subdomain' => 'sales']);
11341134
$routes->add('/objects/(:alphanum)', 'App::objectsList/$1');
@@ -1156,6 +1156,20 @@ public function testWithDifferentSubdomainMissing()
11561156
$this->assertSame($expects, $routes->getRoutes());
11571157
}
11581158

1159+
/**
1160+
* @see https://github.com/codeigniter4/CodeIgniter4/issues/5959
1161+
*/
1162+
public function testWithNoSubdomainAndDot()
1163+
{
1164+
$_SERVER['HTTP_HOST'] = 'example.com';
1165+
1166+
$routes = $this->getCollector();
1167+
1168+
$routes->add('/objects/(:alphanum)', 'App::objectsList/$1', ['subdomain' => '*']);
1169+
1170+
$this->assertSame([], $routes->getRoutes());
1171+
}
1172+
11591173
/**
11601174
* @see https://github.com/codeigniter4/CodeIgniter4/issues/1692
11611175
*/
@@ -1448,7 +1462,7 @@ public function testRouteToWithGenericSubdomainNot()
14481462

14491463
$routes->get('i/(:any)', 'App\Controllers\Site\CDoc::item/$1', ['subdomain' => '*', 'as' => 'doc_item']);
14501464

1451-
$this->assertSame('/i/sth', $routes->reverseRoute('doc_item', 'sth'));
1465+
$this->assertFalse($routes->reverseRoute('doc_item', 'sth'));
14521466
}
14531467

14541468
public function testRouteToWithoutSubdomainMatch()

0 commit comments

Comments
 (0)