Skip to content

Commit 9c1ac0b

Browse files
committed
feat: multiple hostname routing
1 parent f7c471d commit 9c1ac0b

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

system/Router/RouteCollection.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1571,6 +1571,13 @@ private function checkHostname($hostname): bool
15711571
return false;
15721572
}
15731573

1574+
// Has multiple hostname
1575+
if (is_array($hostname)) {
1576+
$hostnameLower = array_map('strtolower', $hostname);
1577+
1578+
return in_array(strtolower($this->httpHost), $hostnameLower, true);
1579+
}
1580+
15741581
return strtolower($this->httpHost) === strtolower($hostname);
15751582
}
15761583

tests/system/Router/RouteCollectionTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
namespace CodeIgniter\Router;
1515

16+
use App\Controllers\Home;
1617
use App\Controllers\Product;
1718
use CodeIgniter\Config\Services;
1819
use CodeIgniter\controller;
@@ -1744,6 +1745,46 @@ public function testRouteOverwritingMatchingHost(): void
17441745
$this->assertSame($expects, $router->handle('/'));
17451746
}
17461747

1748+
public function testRouteMatchingHostMultipleCorrect(): void
1749+
{
1750+
service('superglobals')->setServer('HTTP_HOST', 'two.domain.com');
1751+
service('request')->setMethod(Method::GET);
1752+
1753+
$routes = $this->getCollector();
1754+
$router = new Router($routes, Services::request());
1755+
1756+
$routes->setDefaultNamespace('App\Controllers');
1757+
$routes->setDefaultController('Home');
1758+
$routes->setDefaultMethod('index');
1759+
1760+
$routes->get('/', 'Home::index', ['as' => 'ddd']);
1761+
$routes->get('/', '\App\Controllers\Site\CDoc::index', ['hostname' => ['one.domain.com', 'two.domain.com', 'three.domain.com']]);
1762+
1763+
$expects = '\App\Controllers\Site\CDoc';
1764+
1765+
$this->assertSame($expects, $router->handle('/'));
1766+
}
1767+
1768+
public function testRouteMatchingHostMultipleFail(): void
1769+
{
1770+
service('superglobals')->setServer('HTTP_HOST', 'doc.domain.com');
1771+
service('request')->setMethod(Method::GET);
1772+
1773+
$routes = $this->getCollector();
1774+
$router = new Router($routes, Services::request());
1775+
1776+
$routes->setDefaultNamespace('App\Controllers');
1777+
$routes->setDefaultController('Home');
1778+
$routes->setDefaultMethod('index');
1779+
1780+
$routes->get('/', 'Home::index', ['as' => 'ddd']);
1781+
$routes->get('/', '\App\Controllers\Site\CDoc::index', ['hostname' => ['one.domain.com', 'two.domain.com', 'three.domain.com']]);
1782+
1783+
$expects = '\\' . Home::class;
1784+
1785+
$this->assertSame($expects, $router->handle('/'));
1786+
}
1787+
17471788
/**
17481789
* Tests for router DefaultNameSpace issue
17491790
*

0 commit comments

Comments
 (0)