skip to Main Content

I have a problem working with URI routing in Codeigniter 4.2.6. I have Controller name Home and a method name getIndex. When accessing http://localhost:8080 all working fine. When I try http://localhost:8080/home/index, a message 'Cannot access the default controller "Home" with the controller name URI path' comes up. I set $routes->setAutoRoute(true); and public bool $autoRoutesImproved = true;. The same problem when I create another method getAbout. Accessing http://localhost:8080/home/about resulting a message Cannot accessing... as well.

The same problem when using Sub directory to separate logic. This is my code of subdirectory name Admin :

<?php

namespace AppControllersAdmin;
use AppControllersBaseController;

class Home extends BaseController
{
    public function getIndex()
    {
        # code...
    }

    public function getAbout()
    {
        echo 'This is '.__METHOD__;
    }
}

and trying to access it get the same result Cannot access the default controller "Home" with the controller name URI path.

So how to work with URI Routing in codeigniter 4 especially 4.2.6 using Auto Routing enable and Manual Routing ?

Thank you in advance.

UPDATE

This is my Routes.php

<?php

namespace Config;

// 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('AppControllers');
$routes->setDefaultController('Home');
$routes->setDefaultMethod('index');
$routes->setTranslateURIDashes(false);
$routes->set404Override();
// ...
// 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.
$routes->setAutoRoute(true);

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

// We get a performance increase by specifying the default
// route since we don't have to scan directories.
$routes->get('/', 'Home::index');

2

Answers


    1. File: app/Config/Feature.php
    public bool $autoRoutesImproved = true;
    
    1. File: app/Config/Routes.php
    $routes->setDefaultNamespace('AppControllersAdmin');
    $routes->setDefaultController('AppControllersAdminHome');
    $routes->setDefaultMethod('index');
    $routes->setTranslateURIDashes(false);
    $routes->set404Override();
    // 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.
    $routes->setAutoRoute(true);
    
    // We get a performance increase by specifying the default
    // route since we don't have to scan directories.
    $routes->addRedirect('/', '/home');
    

    Changes made: https://github.com/steven7mwesigwa/ci4.2.7/commit/2ec6853716029d5704933ed95622875fb3fa6728

    Sample repo: https://github.com/steven7mwesigwa/ci4.2.7

    Addendum

    A. In your web browser instead of: http://localhost:8080/home/index, use http://localhost:8080/home

    B. http://localhost:8080/home/about. This URI should work just fine without any "user-defined route".

    Login or Signup to reply.
  1. Step 1
    Edit in file app/Config/Feature.php

    public bool $autoRoutesImproved = true;
    

    to

    public bool $autoRoutesImproved = false;
    

    Step 2
    Edit in file app/Config/Routes.php

    $routes->setAutoRoute(false);
    

    to

    $routes->setAutoRoute(true);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search