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...
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.
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? 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.
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 ..
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.
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.
php spark make:controller /Subfolder/ControllerName
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With