Search code examples
phpcodeignitercodeigniter-4

Ci4 Routes in Subfolders


I know there are several question regarding this but still can't get the answer and still got the problem.

So basically i am using Ci4 routing I have my controller found under:

app
-Controllers
--Admin
---Access
----Departments.php

I made the route in the routes.php as follows:

$routes->group('admin', static function($routes) {
    $routes->group('access', static function($routes) {
        $routes->group('departments', static function($routes) {
            $routes->get('/', 'admin\Access\Departments::index');
        });
    });
});

I have tried also:

$routes->group('admin/access/departments', static function($routes) 
{
    $routes->get('/', 'admin\Access\Departments::index');
});

I have found that i must remove admin\Access\ as the namespace is already defined but still got the same error.

And i am still getting the 404 error:

Controller or its method is not found: \App\Controllers\admin\Access\Departments::index

And yes the controller has a method called index:

<?php

namespace App\Controllers;
class Departments extends BaseController
{
    public function index()
    {
        echo 'departments';
    }
}

Solution

  • Ok got it the problem was in the controller. The namespace in the controller is wrong

    Here is the controller:

    <?php
    
    namespace App\Controllers\Admin\Access;
    use App\Controllers\BaseController;
    class Departments extends BaseController
    {
        public function index()
        {
            echo 'departments';
        }
    }
    

    I changed the namespace, and also added the use of the base controller now otherwise it will not be found.