3

I am using codeigniter(3.1.5) and Ihave two controllers in my application/controllers/ folder. with name controller A and Controller B. I want to extends Controller A in Controller B so that I can use methods of Controller A. But it generates class not found error.

Here is my sample code:

A_Controller.php:

defined('BASEPATH') OR exit('No direct script access allowed');
class A_Controller extends CI_Controller {
  public function index()
  {
  }

  public function display(){
     echo 'base controller function called.';
  }
 }

B_Controller.php:

 defined('BASEPATH') OR exit('No direct script access allowed');
 class B_Controller extends A_Controller {

 }

I want to execute display() method of controller A in controller B. If i put controller A in application/core/ folder and in application/config/config.php file make

$config['subclass_prefix'] = 'A_';

then I can able to access methods of controller A.

Please suggest. Thanks in advance.

Abhishek
  • 583
  • 5
  • 16

5 Answers5

3

Extending a controller in another controller is not really good. Building with MVC and especially with CI, you have other options to achieve this.

  1. Use a class MY_Controller inside application/core that extends the CI_Controller. Later, all (or some) of your controllers should extend the MY_Controller. In MY_Controller you can have many functions and you can call which function you want in your controller.

  2. Use a library. Write your own library in application/libraries and load it in your controller wherever you want. A library is a class with functionality for your project.

  3. Use a helper. Write your own helper in application/helpers and load it in your controller. A helper should have a general purpose for your application.

    In that way, your code will be more flexible and reusable for the future. Messing with 2 Controllers seems bad to me. Remember that with the default Routing system of CI you can be confused.

Community
  • 1
  • 1
GeorgeGeorgitsis
  • 1,262
  • 13
  • 29
  • Yes, you told me standard way but in my case I want to inherit properety of base class into child class just for multisite pupose. Thanks. – Abhishek Jul 07 '17 at 08:39
1

Try to use following code.

defined('BASEPATH') OR exit('No direct script access allowed');
     class B_Controller extends A_Controller {
        public function test()
        {
            $this->display();
        }
     }
B2725
  • 352
  • 1
  • 8
  • thanks for reply... but problem is differrent, it gives 'Class not found' error to extends A_Controller. – Abhishek Jul 07 '17 at 07:09
0

Use construct in controller

defined('BASEPATH') OR exit('No direct script access allowed');
 class B_Controller extends A_Controller {

    public function __construct() {
         parent::__construct();
    }
    public function test()
    {
        $this->display();
    }
 }
B2725
  • 352
  • 1
  • 8
0

I found the solution using including parent controller on child controller like this -

require_once(APPPATH."modules/frontend/controllers/Frontend.php");

then my function like this -


class Home extends Frontend {

    function __construct() {
        parent::__construct();
    }

    function index() {
        echo $this->test(); //from Frontend controller
    }

}

I hope this will help.

pikachu
  • 36
  • 5
0

add this script in B_Controller :

include_once (dirname(__FILE__) . "/A_Controller.php");

for example B_Controller.php :

defined('BASEPATH') OR exit('No direct script access allowed');
include_once (dirname(__FILE__) . "/A_Controller.php");
class B_Controller extends A_Controller {

 }
David Buck
  • 3,752
  • 35
  • 31
  • 35