Bug: TypeError when translateURIDashes is enabled with routes
PHP Version
8.2
CodeIgniter4 Version
4.5.5
CodeIgniter4 Installation Method
Composer (using codeigniter4/appstarter)
Which operating systems have you tested for this bug?
Windows, Linux
Which server did you use?
apache
Database
MySQL (ver. 8.3.0)
What happened?
When setting the translateURIDashes property to true in the Routes.php file of a CodeIgniter 4 application and defining an options route using a closure, a TypeError is triggered.
The error message indicates that the str_replace() function is receiving a Closure instead of the expected string or array.
Error Details:
{
"title": "TypeError",
"type": "TypeError",
"code": 500,
"message": "str_replace(): Argument #3 ($subject) must be of type array|string, Closure given",
"file": "vendor\\codeigniter4\\framework\\system\\Router\\Router.php",
"line": 251
}
Steps to Reproduce
-
Modify
Routes.php:- Open the
app/Config/Routes.phpfile in your CodeIgniter 4 project. - Set the
translateURIDashesproperty totrueas shown below:/** * For Auto Routing. * Whether to translate dashes in URIs for controller/method to underscores. * Primarily useful when using the auto-routing. * * Default: false */ public bool $translateURIDashes = true;
- Open the
-
Define an
optionsRoute:- Add the following route definition using a closure:
$routes->options('(:any)', static function () {});
- Add the following route definition using a closure:
-
Trigger the Route:
- Access any
OPTIONSrequest to the defined route in your application (e.g., using a tool like Postman or cURL).
- Access any
-
Observe the Error:
- A
TypeErrorwill be thrown, indicating thatstr_replace()received aClosureinstead of a valid type.
- A
Expected Output
The application should handle the OPTIONS request gracefully without throwing any errors. The closure provided should be executed as intended, allowing for proper handling of OPTIONS HTTP method requests.
Actual Output
A TypeError is thrown with the following message:
str_replace(): Argument #3 ($subject) must be of type array|string, Closure given
This error points to line 251 in Router.php within the CodeIgniter framework.
Anything else?
No response