Skip to content

Commit 5951c37

Browse files
committed
Allow domain/sub-domain routes to overwrite existing routes. #1692
1 parent 415b805 commit 5951c37

2 files changed

Lines changed: 29 additions & 2 deletions

File tree

system/Router/RouteCollection.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1232,7 +1232,8 @@ protected function fillRouteParams(string $from, array $params = null): string
12321232
*/
12331233
protected function create(string $verb, string $from, $to, array $options = null)
12341234
{
1235-
$prefix = is_null($this->group) ? '' : $this->group . '/';
1235+
$overwrite = false;
1236+
$prefix = is_null($this->group) ? '' : $this->group . '/';
12361237

12371238
$from = filter_var($prefix . $from, FILTER_SANITIZE_STRING);
12381239

@@ -1253,6 +1254,8 @@ protected function create(string $verb, string $from, $to, array $options = null
12531254
{
12541255
return;
12551256
}
1257+
1258+
$overwrite = true;
12561259
}
12571260

12581261
// Limiting to subdomains?
@@ -1264,6 +1267,8 @@ protected function create(string $verb, string $from, $to, array $options = null
12641267
{
12651268
return;
12661269
}
1270+
1271+
$overwrite = true;
12671272
}
12681273

12691274
// Are we offsetting the binds?
@@ -1312,7 +1317,7 @@ protected function create(string $verb, string $from, $to, array $options = null
13121317
// routes should always be the "source of truth".
13131318
// this works only because discovered routes are added just prior
13141319
// to attempting to route the request.
1315-
if (isset($this->routes[$verb][$name]))
1320+
if (isset($this->routes[$verb][$name]) && ! $overwrite)
13161321
{
13171322
return;
13181323
}

tests/system/Router/RouteCollectionTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -771,6 +771,28 @@ public function testWithSubdomain()
771771

772772
$_SERVER['HTTP_HOST'] = 'adm.example.com';
773773

774+
$routes->add('/objects/(:alphanum)', 'Admin::objectsList/$1', ['subdomain' => 'adm']);
775+
$routes->add('/objects/(:alphanum)', 'App::objectsList/$1');
776+
777+
$expects = [
778+
'objects/([a-zA-Z0-9]+)' => '\Admin::objectsList/$1',
779+
];
780+
781+
$this->assertEquals($expects, $routes->getRoutes());
782+
}
783+
784+
//--------------------------------------------------------------------
785+
786+
/**
787+
* @see https://github.com/codeigniter4/CodeIgniter4/issues/1692
788+
*/
789+
public function testWithSubdomainOrdered()
790+
{
791+
$routes = $this->getCollector();
792+
793+
$_SERVER['HTTP_HOST'] = 'adm.example.com';
794+
795+
$routes->add('/objects/(:alphanum)', 'App::objectsList/$1');
774796
$routes->add('/objects/(:alphanum)', 'Admin::objectsList/$1', ['subdomain' => 'adm']);
775797

776798
$expects = [

0 commit comments

Comments
 (0)