3

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 App\Controllers\Admin;
use App\Controllers\BaseController;

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('App\Controllers');
$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');
MSI
  • 165
  • 10

2 Answers2

1
  1. File: app/Config/Feature.php
public bool $autoRoutesImproved = true;
  1. File: app/Config/Routes.php
$routes->setDefaultNamespace('\App\Controllers\Admin');
$routes->setDefaultController('\App\Controllers\Admin\Home');
$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".

steven7mwesigwa
  • 5,701
  • 3
  • 20
  • 34
  • Thank you. I will check with other scenarios first... – MSI Oct 11 '22 at 03:18
  • ```http://localhost:8080/home/about``` showing a message ```Controller or its method is not found: \App\Controllers\Admin\Home\About\Home::getIndex``` when using ```$routes->setDefaultNamespace('App\Controllers\Admin')```. Without changing setDefaultNamespace, throw a message ```Cannot access the default controller "Home" with the controller name URI path.```. Am I missing something ? Why the default controller cannot be accessed through URI? – MSI Oct 11 '22 at 04:21
  • @steven7mwesigma Thank you steven, your help is much appreciated! +1 for the repo – MSI Oct 11 '22 at 14:49
-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);
Aymen
  • 841
  • 1
  • 12
  • 21
  • Well, this works, thank you. But so ... what is for `"Use improved new auto routing instead of the default legacy version."` in the `Feature.php` config file? File hinted by the `Routes.php` config file when you'd like to enable `$routes->setAutoRoute(true);` . If you know it, can you kindly give some details? Thank you – Robert Feb 12 '23 at 09:54
  • Best way to add security issues, **do not do that!** The improved routing is not just a new option, it's so important to replace the legacy version that CI4 devs decided it was worth breaking back-compatibility. – SteeveDroz Mar 21 '23 at 05:33