From 1fab815808fead2cd6ce0d6f8638f276bfb949ed Mon Sep 17 00:00:00 2001 From: kenjis Date: Fri, 29 Apr 2022 19:48:02 +0900 Subject: [PATCH 1/4] docs: add note --- system/Helpers/url_helper.php | 3 +++ 1 file changed, 3 insertions(+) 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 From 2a585bf852100d62f4bfbf21297ad5e3796844e6 Mon Sep 17 00:00:00 2001 From: kenjis Date: Fri, 29 Apr 2022 19:49:37 +0900 Subject: [PATCH 2/4] fix: reverseRoute() does not take into account the default namespace --- system/Router/RouteCollection.php | 9 ++++++++ tests/system/Router/RouteCollectionTest.php | 24 +++++++++++++++++++++ 2 files changed, 33 insertions(+) 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(); From 83720c7074782a364fe56061efdca3e5b14299ac Mon Sep 17 00:00:00 2001 From: kenjis Date: Fri, 29 Apr 2022 19:50:19 +0900 Subject: [PATCH 3/4] docs: fix incorrect sample By default, 'App\Controllers\Galleries::showUserGallery/$1/$2' means 'App\Controllers\App\Controllers\Galleries::showUserGallery/$1/$2'. --- user_guide_src/source/incoming/routing/029.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 From 0e060c8bfefb09d69035b280139fbbcae302018e Mon Sep 17 00:00:00 2001 From: kenjis Date: Fri, 29 Apr 2022 20:01:09 +0900 Subject: [PATCH 4/4] docs: add note --- user_guide_src/source/general/common_functions.rst | 2 ++ user_guide_src/source/helpers/url_helper.rst | 2 ++ 2 files changed, 4 insertions(+) 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