codeigniterroutesautocodeigniter-4

how to enable Auto Routing in CodeIgniter 4?


I want auto routing in codeignitor 4 but don't know how to do it.

As said in the Routes.php file, I have set the "setAutoRoute" to true and also set $autoRoutesImproved to true in "app/Config/Feature.php" but always show 404 error.

app/Config/Routes.php

$routes->setAutoRoute(true);

app/Config/Feature.php

public bool $autoRoutesImproved = true;

I want URL to be like http://localhost/codeigniter4/pages "pages" is my new controller.


Solution

  • http://localhost/codeigniter4/pages

    The route above would mean:


    You got it wrong because you were expecting "pages" to be the controller's name, which isn't the case.


    Auto Routing (Improved)

    When no defined route is found that matches the URI, the system will attempt to match that URI against the controllers and methods when Auto Routing is enabled.

    URI Segments

    The segments in the URL, in following with the Model-View-Controller approach, usually represent:

    example.com/class/method/ID
    
    1. The first segment represents the controller class that should be invoked.

    2. The second segment represents the class method that should be called.

    3. The third, and any additional segments, represent the ID and any variables that will be passed to the controller.