Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: [Auto Routing Improved] Default Method Fallback does not work with $translateUriToCamelCase #8980

Conversation

kenjis
Copy link
Member

@kenjis kenjis commented Jun 21, 2024

Description

Navigate to http://localhost:8080/product/15/edit.
Before:

404
"\App\Controllers\Product::get15()" is not found.

After:

$id string (2) "15"
$action string (4) "edit"
⧉ Called from .../app/Controllers/Product.php:12 [dd()]
<?php

namespace App\Controllers;

use App\Controllers\BaseController;
use CodeIgniter\HTTP\ResponseInterface;

class Product extends BaseController
{
    public function getIndex($id = null, $action = '')
    {
        dd($id, $action);
    }
}
diff --git a/app/Config/Feature.php b/app/Config/Feature.php
index efd4a0b20a..3e9e009657 100644
--- a/app/Config/Feature.php
+++ b/app/Config/Feature.php
@@ -12,7 +12,7 @@ class Feature extends BaseConfig
     /**
      * Use improved new auto routing instead of the default legacy version.
      */
-    public bool $autoRoutesImproved = false;
+    public bool $autoRoutesImproved = true;
 
     /**
      * Use filter execution order in 4.4 or before.
diff --git a/app/Config/Routing.php b/app/Config/Routing.php
index 7abadc7b76..63d4ca2290 100644
--- a/app/Config/Routing.php
+++ b/app/Config/Routing.php
@@ -94,7 +94,7 @@ class Routing extends BaseRouting
      *
      * If FALSE, will stop searching and do NO automatic routing.
      */
-    public bool $autoRoute = false;
+    public bool $autoRoute = true;
 
     /**
      * For Defined Routes.
@@ -136,5 +136,5 @@ class Routing extends BaseRouting
      *
      * Default: false
      */
-    public bool $translateUriToCamelCase = false;
+    public bool $translateUriToCamelCase = true;
 }

Checklist:

  • Securely signed commits
  • Component(s) with PHPDoc blocks, only if necessary or adds value
  • Unit testing, with >80% coverage
  • User guide updated
  • Conforms to style guide

@kenjis kenjis added the bug Verified issues on the current code behavior or pull requests that will fix them label Jun 21, 2024
@kenjis kenjis force-pushed the fix-AutoRouterImproved-translateUriToCamelCase branch from 0d96fba to ea5a585 Compare June 21, 2024 03:56
if (! in_array($method, get_class_methods($this->controller), true)) {
// If `getSomeMethod()` exists, only `controller/some-method` should be
// accessible. But if a visitor navigates to `controller/somemethod`,
// `getSomemethod()` will be checked, and method_exists() will return true.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// `getSomemethod()` will be checked, and method_exists() will return true.
// `getSomeMethod()` will be checked, and method_exists() will return true.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Auto Routing Improved tries to convert a URI to a controller class/method.

If controller/somemethod comes, it will try to check Controller::getSomemethod() (not Controller::getSomeMethod()). Because it adds prefix get and camelizes the URI segment for method.

And in this exaple, there is only Controller::getSomeMethod(), but method_exists() returns true because method names in PHP are case-insensitive.

Copy link
Member Author

@kenjis kenjis Jun 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Improved the comment. d474187

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the explanation.

@kenjis kenjis force-pushed the fix-AutoRouterImproved-translateUriToCamelCase branch 2 times, most recently from 794c0e3 to 497b499 Compare June 24, 2024 00:38
@kenjis kenjis force-pushed the fix-AutoRouterImproved-translateUriToCamelCase branch from 73dd2e7 to d474187 Compare June 24, 2024 00:41
@kenjis kenjis merged commit eaa4339 into codeigniter4:develop Jun 24, 2024
41 checks passed
@kenjis kenjis deleted the fix-AutoRouterImproved-translateUriToCamelCase branch June 24, 2024 09:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Verified issues on the current code behavior or pull requests that will fix them
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants