Skip to content

Commit

Permalink
Merge pull request #5961 from kenjis/fix-route-subdomain
Browse files Browse the repository at this point in the history
fix: route limit to subdomains does not work
  • Loading branch information
kenjis committed May 8, 2022
2 parents 1807ede + fbc94c1 commit a7fe439
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
6 changes: 3 additions & 3 deletions system/Router/RouteCollection.php
Expand Up @@ -1297,7 +1297,7 @@ private function getMethodParams(string $from): string
* Compares the subdomain(s) passed in against the current subdomain
* on this page request.
*
* @param mixed $subdomains
* @param string|string[] $subdomains
*/
private function checkSubdomains($subdomains): bool
{
Expand Down Expand Up @@ -1330,7 +1330,7 @@ private function checkSubdomains($subdomains): bool
* It's especially not perfect since it's possible to register a domain
* with a period (.) as part of the domain name.
*
* @return mixed
* @return false|string the subdomain
*/
private function determineCurrentSubdomain()
{
Expand All @@ -1351,7 +1351,7 @@ private function determineCurrentSubdomain()
}

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

// Account for .co.uk, .co.nz, etc. domains
if (end($host) === 'co') {
Expand Down
18 changes: 16 additions & 2 deletions tests/system/Router/RouteCollectionTest.php
Expand Up @@ -1128,7 +1128,7 @@ public function testWithDotCoSubdomain()
{
$routes = $this->getCollector();

$_SERVER['HTTP_HOST'] = 'example.uk.co';
$_SERVER['HTTP_HOST'] = 'example.co.uk';

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

/**
* @see https://github.com/codeigniter4/CodeIgniter4/issues/5959
*/
public function testWithNoSubdomainAndDot()
{
$_SERVER['HTTP_HOST'] = 'example.com';

$routes = $this->getCollector();

$routes->add('/objects/(:alphanum)', 'App::objectsList/$1', ['subdomain' => '*']);

$this->assertSame([], $routes->getRoutes());
}

/**
* @see https://github.com/codeigniter4/CodeIgniter4/issues/1692
*/
Expand Down Expand Up @@ -1448,7 +1462,7 @@ public function testRouteToWithGenericSubdomainNot()

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

$this->assertSame('/i/sth', $routes->reverseRoute('doc_item', 'sth'));
$this->assertFalse($routes->reverseRoute('doc_item', 'sth'));
}

public function testRouteToWithoutSubdomainMatch()
Expand Down

0 comments on commit a7fe439

Please sign in to comment.