How to work with subdirectory controllers in CodeIgniter 4?
Asked Answered
W

5

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.

Winchester answered 10/4, 2020 at 2:43 Comment(0)
W
20

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.

Winchester answered 10/4, 2020 at 3:48 Comment(4)
And with that knowledge you can implement HMVC like structures and anything you like.Countdown
How did you map the routes ?Couthie
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 routesCollarbone
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.Burwell
Y
2
php spark make:controller /Subfolder/ControllerName
Yearlong answered 3/9, 2021 at 11:43 Comment(2)
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.Agrostology
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.Burwell
R
0
$routes->add('/(.+?)_(.+?)/(.+?)$', 'subdir\\\\$1_$2::$3');
$routes->add('/(.+?)_(.+?)$', 'subdir\\\\$1_$2::index');

I was able to map with this setting.

Rabbitry answered 9/12, 2021 at 9:37 Comment(0)
L
0

The route mapping could be as simple as:

$routes->group('admin', static function ($routes) {
    $routes->get('dashboard', 'Admin\Dashboard::index');
});
Lactoprotein answered 25/12, 2022 at 23:19 Comment(0)
J
0

I faced the same issue, and basically, you need to be careful about two things:

  1. The namespace definition should be with the name of the folder, as:

    namespace App\Controllers\FolderName;

  2. The route should include the folder as well and it should be with a backslash, as:

    $routes->get('somePath/(:segment)', 'FolderName\ControllerName::methodName/$1');

Jessalin answered 7/3 at 13:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.