Skip to content

Commit

Permalink
Merge pull request #9150 from ddevsr/router-multiple-hostname
Browse files Browse the repository at this point in the history
feat: multiple hostname routing
  • Loading branch information
kenjis authored Aug 30, 2024
2 parents 675c819 + 105c1f8 commit b45f151
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 1 deletion.
9 changes: 8 additions & 1 deletion system/Router/RouteCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -1562,7 +1562,7 @@ protected function create(string $verb, string $from, $to, ?array $options = nul
* Compares the hostname passed in against the current hostname
* on this page request.
*
* @param string $hostname Hostname in route options
* @param list<string>|string $hostname Hostname in route options
*/
private function checkHostname($hostname): bool
{
Expand All @@ -1571,6 +1571,13 @@ private function checkHostname($hostname): bool
return false;
}

// Has multiple hostnames
if (is_array($hostname)) {
$hostnameLower = array_map('strtolower', $hostname);

return in_array(strtolower($this->httpHost), $hostnameLower, true);
}

return strtolower($this->httpHost) === strtolower($hostname);
}

Expand Down
41 changes: 41 additions & 0 deletions tests/system/Router/RouteCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace CodeIgniter\Router;

use App\Controllers\Home;
use App\Controllers\Product;
use CodeIgniter\Config\Services;
use CodeIgniter\controller;
Expand Down Expand Up @@ -1744,6 +1745,46 @@ public function testRouteOverwritingMatchingHost(): void
$this->assertSame($expects, $router->handle('/'));
}

public function testRouteMatchingHostMultipleCorrect(): void
{
service('superglobals')->setServer('HTTP_HOST', 'two.domain.com');
service('request')->setMethod(Method::GET);

$routes = $this->getCollector();
$router = new Router($routes, Services::request());

$routes->setDefaultNamespace('App\Controllers');
$routes->setDefaultController('Home');
$routes->setDefaultMethod('index');

$routes->get('/', 'Home::index', ['as' => 'home']);
$routes->get('/', '\App\Controllers\Site\CDoc::index', ['hostname' => ['one.domain.com', 'two.domain.com', 'three.domain.com']]);

$expect = '\App\Controllers\Site\CDoc';

$this->assertSame($expect, $router->handle('/'));
}

public function testRouteMatchingHostMultipleFail(): void
{
service('superglobals')->setServer('HTTP_HOST', 'doc.domain.com');
service('request')->setMethod(Method::GET);

$routes = $this->getCollector();
$router = new Router($routes, Services::request());

$routes->setDefaultNamespace('App\Controllers');
$routes->setDefaultController('Home');
$routes->setDefaultMethod('index');

$routes->get('/', 'Home::index', ['as' => 'home']);
$routes->get('/', '\App\Controllers\Site\CDoc::index', ['hostname' => ['one.domain.com', 'two.domain.com', 'three.domain.com']]);

$expect = '\\' . Home::class;

$this->assertSame($expect, $router->handle('/'));
}

/**
* Tests for router DefaultNameSpace issue
*
Expand Down
5 changes: 5 additions & 0 deletions user_guide_src/source/changelogs/v4.6.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,11 @@ Commands
arguments.
- The ``spark filter:check`` command now displays filter classnames.

Routing
=======

- Now you can specify multiple hostnames when restricting routes.

Testing
=======

Expand Down
9 changes: 9 additions & 0 deletions user_guide_src/source/incoming/routing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,15 @@ by passing the "hostname" option along with the desired domain to allow it on as
This example would only allow the specified hosts to work if the domain exactly matched **accounts.example.com**.
It would not work under the main site at **example.com**.

Restrict by Multiple Hostnames
------------------------------

.. versionadded:: 4.6.0

Also you can restrict by multiple hostnames, e.g:

.. literalinclude:: routing/073.php

Limit to Subdomains
===================

Expand Down
3 changes: 3 additions & 0 deletions user_guide_src/source/incoming/routing/073.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

$routes->get('from', 'to', ['hostname' => ['s1.example.com', 's2.example.com']]);

0 comments on commit b45f151

Please sign in to comment.