CodeIgniter4 icon indicating copy to clipboard operation
CodeIgniter4 copied to clipboard

Bug: TypeError when translateURIDashes is enabled with routes

Open maniaba opened this issue 1 year ago • 0 comments

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

  1. Modify Routes.php:

    • Open the app/Config/Routes.php file in your CodeIgniter 4 project.
    • Set the translateURIDashes property to true as 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;
      
  2. Define an options Route:

    • Add the following route definition using a closure:
      $routes->options('(:any)', static function () {});
      
  3. Trigger the Route:

    • Access any OPTIONS request to the defined route in your application (e.g., using a tool like Postman or cURL).
  4. Observe the Error:

    • A TypeError will be thrown, indicating that str_replace() received a Closure instead of a valid type.

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

maniaba avatar Oct 02 '24 07:10 maniaba