Skip to content

Commit

Permalink
fix: TypeError for OPTIONS routes when translateURIDashes is enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
maniaba committed Oct 4, 2024
1 parent 118c2c4 commit e31ddeb
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
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

0 comments on commit e31ddeb

Please sign in to comment.