diff --git a/system/Router/Router.php b/system/Router/Router.php index 2bb050de30ae..0cd1614991ee 100644 --- a/system/Router/Router.php +++ b/system/Router/Router.php @@ -247,7 +247,7 @@ public function getFilters(): array */ public function controllerName() { - return $this->translateURIDashes + return $this->translateURIDashes && ! $this->controller instanceof Closure ? str_replace('-', '_', $this->controller) : $this->controller; } diff --git a/tests/system/Router/RouterTest.php b/tests/system/Router/RouterTest.php index bb5302e721dc..5d51bf2fd38c 100644 --- a/tests/system/Router/RouterTest.php +++ b/tests/system/Router/RouterTest.php @@ -13,6 +13,7 @@ namespace CodeIgniter\Router; +use Closure; use CodeIgniter\Config\Factories; use CodeIgniter\Config\Services; use CodeIgniter\Exceptions\PageNotFoundException; @@ -69,6 +70,7 @@ private function createRouteCollection(?Routing $routingConfig = null): void 'posts/(:num)/edit' => 'Blog::edit/$1', 'books/(:num)/(:alpha)/(:num)' => 'Blog::show/$3/$1', 'closure/(:num)/(:alpha)' => static fn ($num, $str) => $num . '-' . $str, + 'closure-dash/(:num)/(:alpha)' => static fn ($num, $str) => $num . '-' . $str, '{locale}/pages' => 'App\Pages::list_all', 'test/(:any)/lang/{locale}' => 'App\Pages::list_all', 'admin/admins' => 'App\Admin\Admins::list_all', @@ -216,6 +218,22 @@ public function testClosures(): void $this->assertSame($expects, '123-alpha'); } + public function testClosuresWithTranslateURIDashes(): void + { + $router = new Router($this->collection, $this->request); + $router->setTranslateURIDashes(true); + + $router->handle('closure-dash/123/alpha'); + $closure = $router->controllerName(); + + $this->assertInstanceOf(Closure::class, $closure); + + $expects = $closure(...$router->params()); + + $this->assertIsCallable($router->controllerName()); + $this->assertSame($expects, '123-alpha'); + } + public function testAutoRouteFindsDefaultControllerAndMethod(): void { $this->collection->setAutoRoute(true); diff --git a/user_guide_src/source/changelogs/v4.5.6.rst b/user_guide_src/source/changelogs/v4.5.6.rst index 44b11b724cb9..2ea1f1ce46b7 100644 --- a/user_guide_src/source/changelogs/v4.5.6.rst +++ b/user_guide_src/source/changelogs/v4.5.6.rst @@ -36,6 +36,8 @@ Bugs Fixed - **Parser:** Fixed bug that caused equal key names to be replaced by the key name defined first. +- **Routing:** Fixed a TypeError in `str_replace()` when `Routing::$translateURIDashes` is set to `true` and a route is defined using a closure. + See the repo's `CHANGELOG.md `_ for a complete list of bugs fixed.