Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to work with subdirectory controllers in CodeIgniter 4?

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.

like image 767
Alon Pini Avatar asked Apr 10 '20 02:04

Alon Pini


People also ask

How do I create sub-directories in CodeIgniter?

If you are building a large application you might want to hierarchically organize or structure your controllers into sub-directories. CodeIgniter permits you to do this. Simply create sub-directories under the main app/Controllers/ , and place your controller classes within them.

What is a controller in CodeIgniter?

What is a Controller? A Controller is simply a class file that handles a HTTP request. URI Routing associates a URI with a controller. Every controller you create should extend CodeIgniter\Controller class. This class provides several features that are available to all of your controllers.

How do I map a URL in CodeIgniter?

Each of your sub-directories may contain a default controller which will be called if the URL contains only the sub-directory. Simply put a controller in there that matches the name of your default controller as specified in your app/Config/Routes.php file. CodeIgniter also permits you to map your URIs using its Defined Route Routing ..

What is the use of Uri in CodeIgniter?

URI Routing associates a URI with a controller. Every controller you create should extend CodeIgniter\Controller class. This class provides several features that are available to all of your controllers. The application’s main Request Instance is always available as a class property, $this->request.


2 Answers

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.

like image 193
Alon Pini Avatar answered Nov 10 '22 13:11

Alon Pini


php spark make:controller /Subfolder/ControllerName
like image 42
pharalel Avatar answered Nov 10 '22 14:11

pharalel