Skip to content

Commit

Permalink
fix: TypeError for routes when translateURIDashes is enabled (#9209)
Browse files Browse the repository at this point in the history
* fix: TypeError for OPTIONS routes when translateURIDashes is enabled

* docs: Update changelog for v4.5.6
  • Loading branch information
maniaba authored Nov 3, 2024
1 parent fc19b69 commit 3435872
Show file tree
Hide file tree
Showing 3 changed files with 21 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
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 @@ -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 <https://github.com/codeigniter4/CodeIgniter4/blob/develop/CHANGELOG.md>`_
for a complete list of bugs fixed.

0 comments on commit 3435872

Please sign in to comment.