diff --git a/system/Router/RouteCollection.php b/system/Router/RouteCollection.php index 8cdeadf0f904..c486f5baab20 100644 --- a/system/Router/RouteCollection.php +++ b/system/Router/RouteCollection.php @@ -1420,7 +1420,7 @@ protected function create(string $verb, string $from, $to, array $options = null } // If no namespace found, add the default namespace - if (is_string($to) && strpos($to, '\\') === false) + if (is_string($to) && (strpos($to, '\\') === false || strpos($to, '\\') > 0)) { $namespace = $options['namespace'] ?? $this->defaultNamespace; $to = trim($namespace, '\\') . '\\' . $to; diff --git a/tests/system/Router/RouteCollectionTest.php b/tests/system/Router/RouteCollectionTest.php index 9439d46fbeb5..2a058d0d7a45 100644 --- a/tests/system/Router/RouteCollectionTest.php +++ b/tests/system/Router/RouteCollectionTest.php @@ -1324,7 +1324,7 @@ public function testRouteOverwritingDifferentSubdomains() $routes->setDefaultMethod('index'); $routes->setHTTPVerb('get'); - $routes->get('/', 'App\Controllers\Site\CDoc::index', ['subdomain' => 'doc', 'as' => 'doc_index']); + $routes->get('/', '\App\Controllers\Site\CDoc::index', ['subdomain' => 'doc', 'as' => 'doc_index']); $routes->get('/', 'Home::index', ['subdomain' => 'dev']); $expects = '\App\Controllers\Site\CDoc'; @@ -1344,7 +1344,7 @@ public function testRouteOverwritingTwoRules() $routes->setDefaultController('Home'); $routes->setDefaultMethod('index'); - $routes->get('/', 'App\Controllers\Site\CDoc::index', ['subdomain' => 'doc', 'as' => 'doc_index']); + $routes->get('/', '\App\Controllers\Site\CDoc::index', ['subdomain' => 'doc', 'as' => 'doc_index']); $routes->get('/', 'Home::index'); // the second rule applies, so overwrites the first @@ -1366,7 +1366,7 @@ public function testRouteOverwritingTwoRulesLastApplies() $routes->setDefaultMethod('index'); $routes->get('/', 'Home::index'); - $routes->get('/', 'App\Controllers\Site\CDoc::index', ['subdomain' => 'doc', 'as' => 'doc_index']); + $routes->get('/', '\App\Controllers\Site\CDoc::index', ['subdomain' => 'doc', 'as' => 'doc_index']); $expects = '\App\Controllers\Site\CDoc'; @@ -1386,7 +1386,7 @@ public function testRouteOverwritingMatchingSubdomain() $routes->setDefaultMethod('index'); $routes->get('/', 'Home::index', ['as' => 'ddd']); - $routes->get('/', 'App\Controllers\Site\CDoc::index', ['subdomain' => 'doc', 'as' => 'doc_index']); + $routes->get('/', '\App\Controllers\Site\CDoc::index', ['subdomain' => 'doc', 'as' => 'doc_index']); $expects = '\App\Controllers\Site\CDoc'; @@ -1406,11 +1406,29 @@ public function testRouteOverwritingMatchingHost() $routes->setDefaultMethod('index'); $routes->get('/', 'Home::index', ['as' => 'ddd']); - $routes->get('/', 'App\Controllers\Site\CDoc::index', ['hostname' => 'doc.domain.com', 'as' => 'doc_index']); + $routes->get('/', '\App\Controllers\Site\CDoc::index', ['hostname' => 'doc.domain.com', 'as' => 'doc_index']); $expects = '\App\Controllers\Site\CDoc'; $this->assertEquals($expects, $router->handle('/')); } + //-------------------------------------------------------------------- + // Tests for router DefaultNameSpace issue + // @see https://github.com/codeigniter4/CodeIgniter4/issues/2423 + + public function testRouteDefaultNameSpace() + { + Services::request()->setMethod('get'); + $routes = $this->getCollector(); + $router = new Router($routes, Services::request()); + + $routes->setDefaultNamespace('App\Controllers'); + $routes->get('/', 'Core\Home::index'); + + $expects = '\App\Controllers\Core\Home'; + + $this->assertEquals($expects, $router->handle('/')); + } + }