Skip to content

Commit ba6fddc

Browse files
authored
Merge pull request #5936 from kenjis/fix-reverse-routing-default-namespace
fix: Reverse Routing does not take into account the default namespace
2 parents aee95af + 0e060c8 commit ba6fddc

6 files changed

Lines changed: 42 additions & 2 deletions

File tree

system/Helpers/url_helper.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,9 @@ function mb_url_title(string $str, string $separator = '-', bool $lowercase = fa
522522
* Get the full, absolute URL to a controller method
523523
* (with additional arguments)
524524
*
525+
* NOTE: This requires the controller/method to
526+
* have a route defined in the routes Config file.
527+
*
525528
* @param mixed ...$args
526529
*
527530
* @throws RouterException

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();

user_guide_src/source/general/common_functions.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,8 @@ Miscellaneous Functions
331331
:param string $method: The named route alias, or name of the controller/method to match.
332332
:param mixed $params: One or more parameters to be passed to be matched in the route.
333333

334+
.. note:: This function requires the controller/method to have a route defined in **app/Config/routes.php**.
335+
334336
Generates a URI relative to the domain name (not **baseUrl**) for you based on either a named route alias,
335337
or a controller::method combination. Will take parameters into effect, if provided.
336338

user_guide_src/source/helpers/url_helper.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,8 @@ The following functions are available:
351351
:returns: Absolute URL
352352
:rtype: string
353353

354+
.. note:: This function requires the controller/method to have a route defined in **app/Config/routes.php**.
355+
354356
Builds an absolute URL to a controller method in your app. Example:
355357

356358
.. literalinclude:: url_helper/021.php
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<?php
22

33
// The route is defined as:
4-
$routes->get('users/(:num)/gallery(:any)', 'App\Controllers\Galleries::showUserGallery/$1/$2');
4+
$routes->get('users/(:num)/gallery(:any)', 'Galleries::showUserGallery/$1/$2');
55

66
?>
77

88
<!-- Generate the relative URL to link to user ID 15, gallery 12: -->
9-
<a href="<?= route_to('App\Controllers\Galleries::showUserGallery', 15, 12) ?>">View Gallery</a>
9+
<a href="<?= route_to('Galleries::showUserGallery', 15, 12) ?>">View Gallery</a>
1010
<!-- Result: '/users/15/gallery/12' -->

0 commit comments

Comments
 (0)