Skip to content

Commit b45f151

Browse files
authored
Merge pull request #9150 from ddevsr/router-multiple-hostname
feat: multiple hostname routing
2 parents 675c819 + 105c1f8 commit b45f151

File tree

5 files changed

+66
-1
lines changed

5 files changed

+66
-1
lines changed

system/Router/RouteCollection.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1562,7 +1562,7 @@ protected function create(string $verb, string $from, $to, ?array $options = nul
15621562
* Compares the hostname passed in against the current hostname
15631563
* on this page request.
15641564
*
1565-
* @param string $hostname Hostname in route options
1565+
* @param list<string>|string $hostname Hostname in route options
15661566
*/
15671567
private function checkHostname($hostname): bool
15681568
{
@@ -1571,6 +1571,13 @@ private function checkHostname($hostname): bool
15711571
return false;
15721572
}
15731573

1574+
// Has multiple hostnames
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' => 'home']);
1761+
$routes->get('/', '\App\Controllers\Site\CDoc::index', ['hostname' => ['one.domain.com', 'two.domain.com', 'three.domain.com']]);
1762+
1763+
$expect = '\App\Controllers\Site\CDoc';
1764+
1765+
$this->assertSame($expect, $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' => 'home']);
1781+
$routes->get('/', '\App\Controllers\Site\CDoc::index', ['hostname' => ['one.domain.com', 'two.domain.com', 'three.domain.com']]);
1782+
1783+
$expect = '\\' . Home::class;
1784+
1785+
$this->assertSame($expect, $router->handle('/'));
1786+
}
1787+
17471788
/**
17481789
* Tests for router DefaultNameSpace issue
17491790
*

user_guide_src/source/changelogs/v4.6.0.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,11 @@ Commands
178178
arguments.
179179
- The ``spark filter:check`` command now displays filter classnames.
180180

181+
Routing
182+
=======
183+
184+
- Now you can specify multiple hostnames when restricting routes.
185+
181186
Testing
182187
=======
183188

user_guide_src/source/incoming/routing.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,15 @@ by passing the "hostname" option along with the desired domain to allow it on as
476476
This example would only allow the specified hosts to work if the domain exactly matched **accounts.example.com**.
477477
It would not work under the main site at **example.com**.
478478

479+
Restrict by Multiple Hostnames
480+
------------------------------
481+
482+
.. versionadded:: 4.6.0
483+
484+
Also you can restrict by multiple hostnames, e.g:
485+
486+
.. literalinclude:: routing/073.php
487+
479488
Limit to Subdomains
480489
===================
481490

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
$routes->get('from', 'to', ['hostname' => ['s1.example.com', 's2.example.com']]);

0 commit comments

Comments
 (0)