Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion system/Router/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
18 changes: 18 additions & 0 deletions tests/system/Router/RouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace CodeIgniter\Router;

use Closure;
use CodeIgniter\Config\Factories;
use CodeIgniter\Config\Services;
use CodeIgniter\Exceptions\PageNotFoundException;
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions user_guide_src/source/changelogs/v4.5.6.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ Deprecations
Bugs Fixed
**********

- **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 <https://github.com/codeigniter4/CodeIgniter4/blob/develop/CHANGELOG.md>`_
for a complete list of bugs fixed.
Loading