Jegan
Jegan

Reputation: 595

How to load more than one controller in another controller in CodeIgniter

How to load more than one controller in another controller in CodeIgniter. The below code is i'm using. But it doesn't working. Only the controller which specified at first was work the second one is not working.

class A extends CI_Controller{
    function __construct(){
        parent::__construct();
        $this->load->controller('B');
        $this->load->controller('C');

    }
}

Upvotes: 2

Views: 763

Answers (2)

Mihai Scurtu
Mihai Scurtu

Reputation: 1478

You shouldn't be loading other controllers. Each request should be handled by a single controller. If you require common behaviour you have the following options:

  1. /application/core/MY_Controller.php and extend that class
  2. Move the behaviour to a model
  3. Move the behaviour to a library or helper

If you are unfamiliar with the MVC pattern, this forum post might help you. It's from an old thread, but the principles still apply.

Upvotes: 2

Naincy
Naincy

Reputation: 2943

There are various methods to do that. One of them: you can try this.

//Load the controller you want

$this->load->library('../controllers/controller_name');

//and can call functions of that controller

$this->controller_name->function_name();

I hope this is helpful!

Upvotes: 1

Related Questions