12

I need help with using sub directory controllers in CodeIgniter 4.

I just can't make it work for some reason.

This is the URL for example: www.example.com/admin/dashboard

In the controllers folder, I created a folder named Admin, and a file named Dashboard.php.

I used this code in Dashboard.php:

namespace App\Controllers;

class Dashboard extends BaseController
{
    public function index()
    {

    }
}

I tried changing the class name to AdminDashboard, Admin_Dashboard, pretty much every logical name but every time I get a 404 error, saying:

Controller or its method is not found: App\Controllers\Admin\Dashboard::index

I know the file itself gets loaded successfully, but I think I don't declare the classname correctly and it keeps throwing me those 404 errors.

The documentation of CI4 isn't providing any information about what the classname should be called unfortunately...


UPDATE #1

I managed to make it work by changing few things:

namespace App\Controllers\Admin;
use CodeIgniter\Controller;

class Dashboard extends Controller
{
    public function index()
    {

    }
}

But now it won't extend the BaseController which has some core functions that I built for my app.

Any ideas to how to make it extend BaseController?

I must admit that I don't have much knowledge about namespacing yet, so that might be the reason for my mistakes.

Alon Pini
  • 581
  • 1
  • 3
  • 19

4 Answers4

18

As I imagined, the problem was that I didn't learn about namespacing. I needed to point the use line at the BaseController location.

namespace App\Controllers\Admin;
use App\Controllers\BaseController;

class Dashboard extends BaseController
{
    public function index()
    {

    }
}

Now www.example.com/admin/dashboard/ goes directly to that index function, as intended.

Alon Pini
  • 581
  • 1
  • 3
  • 19
  • 1
    And with that knowledge you can implement HMVC like structures and anything you like. – TimBrownlaw Apr 10 '20 at 10:10
  • How did you map the routes ? – bob269 Jun 13 '20 at 10:39
  • one more thing is you should not have a controller named Admin in the controllers folder, otherwise it won't work, or you can fix this issue using routes – rufaidulk Nov 15 '20 at 04:46
  • 1. Important thing is for the subfolder name is to be capitalized (first letter). 2. Routes do not need to be changed. 3. Namespace seems to not be case sensitive as at the time of this comment. App/Controller/Admin, app/controller/admin, App/Controller/admin all seem to work fine. – Xylo May 19 '22 at 09:59
2
php spark make:controller /Subfolder/ControllerName
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
pharalel
  • 21
  • 1
  • Welcome to Stack Overflow! Welcome to StackOverflow. While this code may answer the question, providing additional context regarding *how* and/or *why* it solves the problem would improve the answer's long-term value. – Sven Eberth Sep 04 '21 at 23:35
  • php spark creates the properly capitalized subfolder, controller and class name. This takes the improper capitalization mistakes when manually typing the names of folder, controller name and class inside controller. – Xylo May 19 '22 at 10:09
0
$routes->add('/(.+?)_(.+?)/(.+?)$', 'subdir\\\\$1_$2::$3');
$routes->add('/(.+?)_(.+?)$', 'subdir\\\\$1_$2::index');

I was able to map with this setting.

redara
  • 1
0

The route mapping could be as simple as:

$routes->group('admin', static function ($routes) {
    $routes->get('dashboard', 'Admin\Dashboard::index');
});
Doc Hojo
  • 1
  • 2