Skip to content

Commit

Permalink
Merge pull request #2425 from Instrye/patch1
Browse files Browse the repository at this point in the history
fix: Router setDefaultNameSpace can't worker
  • Loading branch information
lonnieezell committed Nov 26, 2019
2 parents 428bf41 + 90d4936 commit 856d816
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
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('/'));
}

}

0 comments on commit 856d816

Please sign in to comment.