Skip to content

Commit a185960

Browse files
committed
fix: reverse routing does not work with full classname starting with \
E.g., url_to('\App\Controllers\backend\Dashboard::index')
1 parent b82d6db commit a185960

2 files changed

Lines changed: 16 additions & 3 deletions

File tree

system/Router/RouteCollection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1008,7 +1008,7 @@ public function reverseRoute(string $search, ...$params)
10081008
$namespace = trim($this->defaultNamespace, '\\') . '\\';
10091009
if (
10101010
substr($search, 0, 1) !== '\\'
1011-
|| substr($search, 0, strlen($namespace)) !== $namespace
1011+
&& substr($search, 0, strlen($namespace)) !== $namespace
10121012
) {
10131013
$search = $namespace . $search;
10141014
}

tests/system/Router/RouteCollectionTest.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use CodeIgniter\Router\Exceptions\RouterException;
1616
use CodeIgniter\Test\CIUnitTestCase;
1717
use Config\Modules;
18+
use Generator;
1819
use Tests\Support\Controllers\Hello;
1920

2021
/**
@@ -855,14 +856,26 @@ public function testReverseRoutingWithLocale()
855856
$this->assertSame('/en/contact', $routes->reverseRoute('myController::goto'));
856857
}
857858

858-
public function testReverseRoutingDefaultNamespaceAppController()
859+
public function reverseRoutingHandlerProvider(): Generator
860+
{
861+
return yield from [
862+
'Omit namespace' => ['Galleries::showUserGallery'],
863+
'Specify full ns starting with /' => ['\App\Controllers\Galleries::showUserGallery'],
864+
'Specify full ns w/o staring /' => ['App\Controllers\Galleries::showUserGallery'],
865+
];
866+
}
867+
868+
/**
869+
* @dataProvider reverseRoutingHandlerProvider
870+
*/
871+
public function testReverseRoutingDefaultNamespaceAppController(string $controller)
859872
{
860873
$routes = $this->getCollector();
861874
$routes->setDefaultNamespace('App\Controllers');
862875

863876
$routes->get('users/(:num)/gallery(:any)', 'Galleries::showUserGallery/$1/$2');
864877

865-
$match = $routes->reverseRoute('Galleries::showUserGallery', 15, 12);
878+
$match = $routes->reverseRoute($controller, 15, 12);
866879

867880
$this->assertSame('/users/15/gallery12', $match);
868881
}

0 commit comments

Comments
 (0)