Skip to content

Commit e31ddeb

Browse files
committed
fix: TypeError for OPTIONS routes when translateURIDashes is enabled
1 parent 118c2c4 commit e31ddeb

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-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);

0 commit comments

Comments
 (0)