Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: multiple hostname routing #9150

Merged
merged 9 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -165,6 +165,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']]);
Loading