Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extend controller in Codeigniter 4?

How can I create my own core controller in codeigniter 4 like codeigniter 3?
Core Controller Codeigniter 3

like image 968
Parag Dhali Avatar asked May 07 '26 05:05

Parag Dhali


1 Answers

When you say "core" controller I understand you to mean a "base" controller which in CI v3 is often named MY_Controller. If that is what you are asking for it's actually much easier in v4 because of namespaces and the autoloader. Also, there is no need for the trickery of using a prefix like MY_.

Here's just how easy it is. The "base" controller...

File: /application/Controllers/Base.php

<?php namespace App\Controllers;

class Base extends \CodeIgniter\Controller
{
    //your code here
}

Then extend the above to create any other controller

File: /application/Controllers/Home.php

<?php namespace App\Controllers;

class Home extends \App\Controllers\Base
{
   // Your code here
}

The Home controller will inherit all the properties and methods you define in Base.

like image 115
DFriend Avatar answered May 09 '26 17:05

DFriend



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!