Welcome Guest, Not a member yet? Register   Sign In
CI 4.18 -> 4.20 causes RouterException
#1

Hi all,
I am upgrading from 4.18 to 4.20.
But now I receieve numerous RouterExceptions.
For example: Controller or its method is not found: \App\Controllers\Login::index
This is de routes.php rule:
PHP Code:
$routes->get('/beheer''Login::index'); 

And I do have a app/Controllers/Login.php file with an index() method in it.
I can't seem to find anything in the changelog what will cause this behaviour.
Reply
#2

Check your routes:

Code:
$ php spark routes
$ php spark routes | grep beheer
Reply
#3

| GET | beheer | \App\Controllers\Login::index | | toolbar |

It's present....
Only difference.... App is uppercase, and my folder name is app (lowecase).
Can that be the problem?
Reply
#4

(This post was last modified: 06-08-2022, 02:09 PM by kenjis.)

(06-08-2022, 09:37 AM)sjender Wrote: Only difference.... App is uppercase, and my folder name is app (lowecase).
Can that be the problem?

No.

Code:
Controller or its method is not found: \App\Controllers\Login::index

This error occurs when the controller class can't be autoloaded.

Did you customize something in autoload configuration?

Quote:I can't seem to find anything in the changelog what will cause this behaviour.

Yes, unless there is a bug we don't know, there is no change in that area.
Reply
#5

(This post was last modified: 06-09-2022, 05:00 AM by sjender.)

I think I've found the problem...
It's an url_to() issue.
I have this line to redirect to the dashboard when a user is already logged in:
PHP Code:
return redirect()->to(url_to('\App\Controllers\backend\Dashboard::index')); 

This line throws the error.
But when I change it to:
PHP Code:
return redirect()->to(url_to('backend\Dashboard::index')); 

It works, but I have many uses of url_to(), it would be quite some work to modify all these occurences, and with a risk of forgetting one.

Does anyone know, why this is a problem all of a sudden?
And is there a way of making it work like it used to work?
Reply
#6

(This post was last modified: 06-09-2022, 05:52 AM by kenjis.)

Sorry, it seems a bug.
'\App\Controllers\backend\Dashboard::index' should work.

Can you try this patch?

Code:
--- a/system/Router/RouteCollection.php
+++ b/system/Router/RouteCollection.php
@@ -1008,7 +1008,7 @@ class RouteCollection implements RouteCollectionInterface
        $namespace = trim($this->defaultNamespace, '\\') . '\\';
        if (
            substr($search, 0, 1) !== '\\'
-            || substr($search, 0, strlen($namespace)) !== $namespace
+            && substr($search, 0, strlen($namespace)) !== $namespace
        ) {
            $search = $namespace . $search;
        }
Reply
#7

Yep, that seems to work!
Thanks.

Is this fix going to be in next versions of CI?
Reply
#8

I sent a PR to fix this: https://github.com/codeigniter4/CodeIgniter4/pull/6104
Reply




Theme © iAndrew 2016 - Forum software by © MyBB