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

fix: Router setDefaultNameSpace can't worker #2425

Merged
merged 1 commit into from
Nov 26, 2019
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
2 changes: 1 addition & 1 deletion system/Router/RouteCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
28 changes: 23 additions & 5 deletions tests/system/Router/RouteCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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
Expand All @@ -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';

Expand All @@ -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';

Expand All @@ -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('/'));
}

}