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

Route can not work: Controller or its method is not found #2299

Closed
brucelin opened this issue Oct 3, 2019 · 4 comments · Fixed by #2327
Closed

Route can not work: Controller or its method is not found #2299

brucelin opened this issue Oct 3, 2019 · 4 comments · Fixed by #2327
Labels
bug Verified issues on the current code behavior or pull requests that will fix them

Comments

@brucelin
Copy link

brucelin commented Oct 3, 2019

When I add a route as the document described:

$routes->add('products/([a-z]+)/(\d+)', '$1::id_$2');

But it shows the error:

Controller or its method is not found: \App\Controllers$1::id_12

CodeIgniter 4 version
rc-2.1

Affected module(s)
Router

Context

  • OS: Win7
  • PHP 7.3.10
  • Spark Server

Maybe Some regular expression replacement error occurred when match the uri.

@jim-parry jim-parry added the bug Verified issues on the current code behavior or pull requests that will fix them label Oct 10, 2019
@jim-parry
Copy link
Contributor

If the first character of the "to" parameter is a dollar sign, as shown, then the first segment is not replaced at all.

In the example reported, and with a test URI "/products/apple/222"

  • a "to" value of "$1::id_$2" results in a controller name of "$1'
  • a "to" value of "$1::id_$2" results in a controller name of "$1'
  • a "to" value of "A$1::id_$2" results in a controller name of "\Aapple'

I can replicate the problem, but have been unable to solve it :(

@jim-parry
Copy link
Contributor

@lonnieezell Is the user guide correct, that the above route should work as descibed.I don't want to bang my head against the wall if this is a writeup issue.

@lonnieezell
Copy link
Member

lonnieezell commented Oct 11, 2019

I think the docs definitely need to be changed to not show that example. It really doesn't make any sense and, if I wrote that, I apologize. :)

While I think what you're trying to do there is technically possible, it's completely a bad idea. You should never let the URI specify the controller to run like that. Let auto-routing do it's magic, because if you start doing what that example shows, then a user can attempt to run any controller/method in the App\Controllers namespace. Especially when used with $routes->add() which works for all HTTP verbs.

So, yes, that example needs to be changed!

For a URI of /products/apple/222 you'd want that to read something more like:

$routes->get('products/([a-z]+)/(\d+)', 'Products::show/$1/$2 );

@brucelin
Copy link
Author

If the first character of the "to" parameter is a dollar sign, as shown, then the first segment is not replaced at all.

In the example reported, and with a test URI "/products/apple/222"

  • a "to" value of "$1::id_$2" results in a controller name of "$1'
  • a "to" value of "$1::id_$2" results in a controller name of "$1'
  • a "to" value of "A$1::id_$2" results in a controller name of "\Aapple'

I can replicate the problem, but have been unable to solve it :(

I read the code and guess the problem may happen because when parsing the url to the controller class, the code join the namespace and controller name like App\Controllers\$1::id_$2. Then the \$1 can not be a correct regular expression. Maybe we can first parse the regular expression $1::id_$2, then attached it with the namespace.

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 a pull request may close this issue.

3 participants