Skip to content

Commit

Permalink
Merge pull request #5936 from kenjis/fix-reverse-routing-default-name…
Browse files Browse the repository at this point in the history
…space

fix: Reverse Routing does not take into account the default namespace
  • Loading branch information
kenjis authored May 1, 2022
2 parents aee95af + 0e060c8 commit ba6fddc
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 2 deletions.
3 changes: 3 additions & 0 deletions system/Helpers/url_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions system/Router/RouteCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
24 changes: 24 additions & 0 deletions tests/system/Router/RouteCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 2 additions & 0 deletions user_guide_src/source/general/common_functions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
2 changes: 2 additions & 0 deletions user_guide_src/source/helpers/url_helper.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions user_guide_src/source/incoming/routing/029.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

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

?>

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

0 comments on commit ba6fddc

Please sign in to comment.