codeigniter4 / CodeIgniter4

Open Source PHP Framework (originally from EllisLab)
https://codeigniter.com/
MIT License
5.35k stars 1.9k forks source link

Bug: improved autorouting doesn't work if class has one routed method #6097

Closed SteeveDroz closed 2 years ago

SteeveDroz commented 2 years ago

PHP Version

8.1

CodeIgniter4 Version

4.2.0

CodeIgniter4 Installation Method

Composer (using codeigniter4/appstarter)

Which operating systems have you tested for this bug?

Windows

Which server did you use?

Nginx

Database

No response

What happened?

The example in the tutorial breaks if I add a defined route for a method in the same controller.

Steps to Reproduce

Following the tutorial on the official website, create a controller called Helloworld with the methods getIndex and getComment.

Create a third method called test (not getTest).

Define a route in app/Config/Routes.php like this:

$routes->add('/test', 'Helloworld::test');

Everythink still works.

Change the route to:

$routes->get('/test', 'Helloworld::test');

The URI /test works, but /helloworld and /helloworld/comment don't (404 Page Not Found).

Expected Output

Adding a defined route to a method should not change the behavior of the auto routing of the other methods of the same controller.

Anything else?

By adding the method Home::getTest, the command php spark routes gives the result:

CodeIgniter v4.2.0 Command Line Tool - Server Time: 2022-06-08 01:20:31 UTC-05:00

ERROR: 404
Cannot access the default controller "Home" with the controller name URI path.
kenjis commented 2 years ago

Thank you for taking time.

This is not a bug. It is the specification for security reason.

Can’t access controllers in Defined Routes. It completely separates controllers accessible via Auto Routes from those accessible via Defined Routes. https://codeigniter.com/user_guide/changelogs/v4.2.0.html#new-improved-auto-routing

Adding a defined route to a method should not change the behavior of the auto routing of the other methods of the same controller.

Why do you think so?

kenjis commented 2 years ago

Create a third method called test (not getTest).

Define a route in app/Config/Routes.php like this:

$routes->add('/test', 'Helloworld::test');

Everythink still works.

If you add the route definition, http://localhost:8080/helloworld and http://localhost:8080/helloworld/comment will be 404.

SteeveDroz commented 2 years ago

Can’t access controllers in Defined Routes. It completely separates controllers accessible via Auto Routes from those accessible via Defined Routes. https://codeigniter.com/user_guide/changelogs/v4.2.0.html#new-improved-auto-routing

Oh, my bad, I missed that part.

Thanks for your answer! It's indeed not a bug.

kenjis commented 2 years ago

@SteeveDroz No problem.

By adding the method Home::getTest, the command php spark routes gives the result:

This part is a bug. I will fix it. Thank you for reporting.