codeignitercodeigniter-4

Codeigniter 4 Autoroutes not working properly


I've been tried follow the the tutorial from the ci4 website how to enable auto routes but it's still not working. Is there something wrong with the code?

setautoroutes

not detected routes

improved autoroutes enable

Pages.php controller

my Controller Pages.php :

<?php

namespace App\Controllers;

use Config\View;

class Pages extends BaseController
{
    public function index()
    {
        $data = [
            'title' => 'Home | PETANI BEDAS'
        ];
        return view('pages/main', $data);
    }

    public function about()
    {
        $data = [
            'title' => 'About Us'
        ];
        return view('pages/about', $data);
    }

    public function contact()
    {
        $data = [
            'title' => 'contact',
            'alamat' => [
                [
                    'tempat' => 'Rumah',
                    'alamat' => 'Jl. Jendral Sudirman No. 54',
                    'kota' => 'Bandung'
                ],
                [
                    'tempat' => 'Kantor',
                    'alamat' => 'Jl. Gatot Subroto No. 14',
                    'kota' => 'Jakarta'
                ]
            ]
        ];
        return view('pages/contact', $data);
    }
}

my Config Routes.php :

<?php

namespace Config;

use App\Controllers\Coba;
use App\Controllers\Home;
use App\Controllers\Pages;
use CodeIgniter\Commands\Utilities\Routes;

// Create a new instance of our RouteCollection class.
$routes = Services::routes();

// Load the system's routing file first, so that the app and ENVIRONMENT
// can override as needed.
if (is_file(SYSTEMPATH . 'Config/Routes.php')) {
    require SYSTEMPATH . 'Config/Routes.php';
}

/*
 * --------------------------------------------------------------------
 * Router Setup
 * --------------------------------------------------------------------
 */
$routes->setDefaultNamespace('App\Controllers');
$routes->setDefaultController('Home');
$routes->setDefaultMethod('index');
$routes->setTranslateURIDashes(false);
$routes->set404Override();
$routes->setAutoRoute(true);

// The Auto Routing (Legacy) is very dangerous. It is easy to create vulnerable apps
// where controller filters or CSRF protection are bypassed.
// If you don't want to define all routes, please use the Auto Routing (Improved).
// Set `$autoRoutesImproved` to true in `app/Config/Feature.php` and set the following to true.


/*
 * --------------------------------------------------------------------
 * Route Definitions
 * --------------------------------------------------------------------
 */

// We get a performance increase by specifying the default
// route since we don't have to scan directories.

$routes->get('/', 'Pages::index');
// $routes->get('/pages/about', 'Pages::about');
// $routes->get('/pages/contact', 'Pages::contact');
// $routes->get('/pages/komik', 'Pages::komik');

/*
 * --------------------------------------------------------------------
 * Additional Routing
 * --------------------------------------------------------------------
 *
 * There will often be times that you need additional routing and you
 * need it to be able to override any defaults in this file. Environment
 * based routes is one such time. require() additional route files here
 * to make that happen.
 *
 * You will have access to the $routes object within that file without
 * needing to reload it.
 */
if (is_file(APPPATH . 'Config/' . ENVIRONMENT . '/Routes.php')) {
    require APPPATH . 'Config/' . ENVIRONMENT . '/Routes.php';
}

my Config Features.php

<?php

namespace Config;

use CodeIgniter\Config\BaseConfig;

/**
 * Enable/disable backward compatibility breaking features.
 */
class Feature extends BaseConfig
{
    /**
     * Enable multiple filters for a route or not.
     *
     * If you enable this:
     *   - CodeIgniter\CodeIgniter::handleRequest() uses:
     *     - CodeIgniter\Filters\Filters::enableFilters(), instead of enableFilter()
     *   - CodeIgniter\CodeIgniter::tryToRouteIt() uses:
     *     - CodeIgniter\Router\Router::getFilters(), instead of getFilter()
     *   - CodeIgniter\Router\Router::handle() uses:
     *     - property $filtersInfo, instead of $filterInfo
     *     - CodeIgniter\Router\RouteCollection::getFiltersForRoute(), instead of getFilterForRoute()
     *
     * @var bool
     */
    public $multipleFilters = false;

    /**
     * Use improved new auto routing instead of the default legacy version.
     */
    public bool $autoRoutesImproved = true;
}


Solution

  • CodeIgniter4 Auto-Routes not working properly

    $routes->setAutoRoute(true); // Config/Routes.php

    public bool $autoRoutesImproved = true; // Config/Feature.php

    If you have followed the configurations properly and still facing issue. then some points need to be in mind-

    1. Make sure you don't have implemented custom routing for the controller in "Config/Routing.php".

    2. Make sure you are forming correct URI /Pages/about as this is case sensitive. Controller name must match the format.

    3. You have to declare controller methods with request method prefixed. For example:-

      public function getAbout() { //for GET Request } public function postLogin() { // for POST Request }