Skip to content

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

Merged
kenjis merged 4 commits into
codeigniter4:developfrom
kenjis:fix-AutoRouterImproved-translateUriToCamelCase
Jun 24, 2024
Merged

fix: [Auto Routing Improved] Default Method Fallback does not work with $translateUriToCamelCase#8980
kenjis merged 4 commits into
codeigniter4:developfrom
kenjis:fix-AutoRouterImproved-translateUriToCamelCase

Conversation

@kenjis

@kenjiskenjis commented Jun 21, 2024

Copy link
Copy Markdown
Member

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()]
<?phpnamespaceApp\Controllers;
useApp\Controllers\BaseController;
useCodeIgniter\HTTP\ResponseInterface;
class Product extends BaseController
{
publicfunctiongetIndex($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

@kenjiskenjis added the bug Verified issues on the current code behavior or pull requests that will fix them label Jun 21, 2024
@kenjis
kenjisforce-pushed the fix-AutoRouterImproved-translateUriToCamelCase branch from 0d96fba to ea5a585CompareJune 21, 2024 03:56
Comment threadsystem/Router/AutoRouterImproved.php Outdated
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
Copy Markdown
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
Copy Markdown
MemberAuthor

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.

@kenjiskenjisJun 24, 2024

Copy link
Copy Markdown
MemberAuthor

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
Copy Markdown
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
kenjisforce-pushed the fix-AutoRouterImproved-translateUriToCamelCase branch 2 times, most recently from 794c0e3 to 497b499CompareJune 24, 2024 00:38
@kenjis
kenjisforce-pushed the fix-AutoRouterImproved-translateUriToCamelCase branch from 73dd2e7 to d474187CompareJune 24, 2024 00:41
@kenjis
kenjis merged commit eaa4339 into codeigniter4:developJun 24, 2024
@kenjis
kenjis deleted the fix-AutoRouterImproved-translateUriToCamelCase branch June 24, 2024 09:32
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bugVerified 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

@kenjis@michalsn@datamweb@ddevsr