diff --git a/system/Helpers/url_helper.php b/system/Helpers/url_helper.php
index e1e8b8a30e32..8efd11c1ea6a 100644
--- a/system/Helpers/url_helper.php
+++ b/system/Helpers/url_helper.php
@@ -522,6 +522,9 @@ function mb_url_title(string $str, string $separator = '-', bool $lowercase = fa
* Get the full, absolute URL to a controller method
* (with additional arguments)
*
+ * NOTE: This requires the controller/method to
+ * have a route defined in the routes Config file.
+ *
* @param mixed ...$args
*
* @throws RouterException
diff --git a/system/Router/RouteCollection.php b/system/Router/RouteCollection.php
index 120ed99c73ee..abfa5493ae1f 100644
--- a/system/Router/RouteCollection.php
+++ b/system/Router/RouteCollection.php
@@ -997,6 +997,15 @@ public function reverseRoute(string $search, ...$params)
}
}
+ // Add the default namespace if needed.
+ $namespace = trim($this->defaultNamespace, '\\') . '\\';
+ if (
+ substr($search, 0, 1) !== '\\'
+ || substr($search, 0, strlen($namespace)) !== $namespace
+ ) {
+ $search = $namespace . $search;
+ }
+
// If it's not a named route, then loop over
// all routes to find a match.
foreach ($this->routes as $collection) {
diff --git a/tests/system/Router/RouteCollectionTest.php b/tests/system/Router/RouteCollectionTest.php
index 7eb3f99f3128..444d32d9b258 100644
--- a/tests/system/Router/RouteCollectionTest.php
+++ b/tests/system/Router/RouteCollectionTest.php
@@ -849,6 +849,30 @@ public function testReverseRoutingWithLocale()
$this->assertSame('/en/contact', $routes->reverseRoute('myController::goto'));
}
+ public function testReverseRoutingDefaultNamespaceAppController()
+ {
+ $routes = $this->getCollector();
+ $routes->setDefaultNamespace('App\Controllers');
+
+ $routes->get('users/(:num)/gallery(:any)', 'Galleries::showUserGallery/$1/$2');
+
+ $match = $routes->reverseRoute('Galleries::showUserGallery', 15, 12);
+
+ $this->assertSame('/users/15/gallery12', $match);
+ }
+
+ public function testReverseRoutingDefaultNamespaceAppControllerSubNamespace()
+ {
+ $routes = $this->getCollector();
+ $routes->setDefaultNamespace('App\Controllers');
+
+ $routes->get('admin/(:num)/gallery(:any)', 'Admin\Galleries::showUserGallery/$1/$2');
+
+ $match = $routes->reverseRoute('Admin\Galleries::showUserGallery', 15, 12);
+
+ $this->assertSame('/admin/15/gallery12', $match);
+ }
+
public function testNamedRoutes()
{
$routes = $this->getCollector();
diff --git a/user_guide_src/source/general/common_functions.rst b/user_guide_src/source/general/common_functions.rst
index f8cbcf69bc53..4b8ef4e92cc2 100755
--- a/user_guide_src/source/general/common_functions.rst
+++ b/user_guide_src/source/general/common_functions.rst
@@ -331,6 +331,8 @@ Miscellaneous Functions
:param string $method: The named route alias, or name of the controller/method to match.
:param mixed $params: One or more parameters to be passed to be matched in the route.
+ .. note:: This function requires the controller/method to have a route defined in **app/Config/routes.php**.
+
Generates a URI relative to the domain name (not **baseUrl**) for you based on either a named route alias,
or a controller::method combination. Will take parameters into effect, if provided.
diff --git a/user_guide_src/source/helpers/url_helper.rst b/user_guide_src/source/helpers/url_helper.rst
index 9f673b88e8bb..c7aff69cb86a 100644
--- a/user_guide_src/source/helpers/url_helper.rst
+++ b/user_guide_src/source/helpers/url_helper.rst
@@ -351,6 +351,8 @@ The following functions are available:
:returns: Absolute URL
:rtype: string
+ .. note:: This function requires the controller/method to have a route defined in **app/Config/routes.php**.
+
Builds an absolute URL to a controller method in your app. Example:
.. literalinclude:: url_helper/021.php
diff --git a/user_guide_src/source/incoming/routing/029.php b/user_guide_src/source/incoming/routing/029.php
index e6bc6bdfb65f..c1a8d5947cd8 100644
--- a/user_guide_src/source/incoming/routing/029.php
+++ b/user_guide_src/source/incoming/routing/029.php
@@ -1,10 +1,10 @@
get('users/(:num)/gallery(:any)', 'App\Controllers\Galleries::showUserGallery/$1/$2');
+$routes->get('users/(:num)/gallery(:any)', 'Galleries::showUserGallery/$1/$2');
?>
-View Gallery
+View Gallery