Skip to content

Commit 2a585bf

Browse files
committed
fix: reverseRoute() does not take into account the default namespace
1 parent 1fab815 commit 2a585bf

2 files changed

Lines changed: 33 additions & 0 deletions

File tree

system/Router/RouteCollection.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -997,6 +997,15 @@ public function reverseRoute(string $search, ...$params)
997997
}
998998
}
999999

1000+
// Add the default namespace if needed.
1001+
$namespace = trim($this->defaultNamespace, '\\') . '\\';
1002+
if (
1003+
substr($search, 0, 1) !== '\\'
1004+
|| substr($search, 0, strlen($namespace)) !== $namespace
1005+
) {
1006+
$search = $namespace . $search;
1007+
}
1008+
10001009
// If it's not a named route, then loop over
10011010
// all routes to find a match.
10021011
foreach ($this->routes as $collection) {

tests/system/Router/RouteCollectionTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -849,6 +849,30 @@ public function testReverseRoutingWithLocale()
849849
$this->assertSame('/en/contact', $routes->reverseRoute('myController::goto'));
850850
}
851851

852+
public function testReverseRoutingDefaultNamespaceAppController()
853+
{
854+
$routes = $this->getCollector();
855+
$routes->setDefaultNamespace('App\Controllers');
856+
857+
$routes->get('users/(:num)/gallery(:any)', 'Galleries::showUserGallery/$1/$2');
858+
859+
$match = $routes->reverseRoute('Galleries::showUserGallery', 15, 12);
860+
861+
$this->assertSame('/users/15/gallery12', $match);
862+
}
863+
864+
public function testReverseRoutingDefaultNamespaceAppControllerSubNamespace()
865+
{
866+
$routes = $this->getCollector();
867+
$routes->setDefaultNamespace('App\Controllers');
868+
869+
$routes->get('admin/(:num)/gallery(:any)', 'Admin\Galleries::showUserGallery/$1/$2');
870+
871+
$match = $routes->reverseRoute('Admin\Galleries::showUserGallery', 15, 12);
872+
873+
$this->assertSame('/admin/15/gallery12', $match);
874+
}
875+
852876
public function testNamedRoutes()
853877
{
854878
$routes = $this->getCollector();

0 commit comments

Comments
 (0)