Skip to content

Commit 3435872

Browse files
authored
fix: TypeError for routes when translateURIDashes is enabled (codeigniter4#9209)
* fix: TypeError for OPTIONS routes when translateURIDashes is enabled * docs: Update changelog for v4.5.6
1 parent fc19b69 commit 3435872

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

system/Router/Router.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ public function getFilters(): array
247247
*/
248248
public function controllerName()
249249
{
250-
return $this->translateURIDashes
250+
return $this->translateURIDashes && ! $this->controller instanceof Closure
251251
? str_replace('-', '_', $this->controller)
252252
: $this->controller;
253253
}

tests/system/Router/RouterTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
namespace CodeIgniter\Router;
1515

16+
use Closure;
1617
use CodeIgniter\Config\Factories;
1718
use CodeIgniter\Config\Services;
1819
use CodeIgniter\Exceptions\PageNotFoundException;
@@ -69,6 +70,7 @@ private function createRouteCollection(?Routing $routingConfig = null): void
6970
'posts/(:num)/edit' => 'Blog::edit/$1',
7071
'books/(:num)/(:alpha)/(:num)' => 'Blog::show/$3/$1',
7172
'closure/(:num)/(:alpha)' => static fn ($num, $str) => $num . '-' . $str,
73+
'closure-dash/(:num)/(:alpha)' => static fn ($num, $str) => $num . '-' . $str,
7274
'{locale}/pages' => 'App\Pages::list_all',
7375
'test/(:any)/lang/{locale}' => 'App\Pages::list_all',
7476
'admin/admins' => 'App\Admin\Admins::list_all',
@@ -216,6 +218,22 @@ public function testClosures(): void
216218
$this->assertSame($expects, '123-alpha');
217219
}
218220

221+
public function testClosuresWithTranslateURIDashes(): void
222+
{
223+
$router = new Router($this->collection, $this->request);
224+
$router->setTranslateURIDashes(true);
225+
226+
$router->handle('closure-dash/123/alpha');
227+
$closure = $router->controllerName();
228+
229+
$this->assertInstanceOf(Closure::class, $closure);
230+
231+
$expects = $closure(...$router->params());
232+
233+
$this->assertIsCallable($router->controllerName());
234+
$this->assertSame($expects, '123-alpha');
235+
}
236+
219237
public function testAutoRouteFindsDefaultControllerAndMethod(): void
220238
{
221239
$this->collection->setAutoRoute(true);

user_guide_src/source/changelogs/v4.5.6.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ Bugs Fixed
3636

3737
- **Parser:** Fixed bug that caused equal key names to be replaced by the key name defined first.
3838

39+
- **Routing:** Fixed a TypeError in `str_replace()` when `Routing::$translateURIDashes` is set to `true` and a route is defined using a closure.
40+
3941
See the repo's
4042
`CHANGELOG.md <https://github.com/codeigniter4/CodeIgniter4/blob/develop/CHANGELOG.md>`_
4143
for a complete list of bugs fixed.

0 commit comments

Comments
 (0)