[codeigniter] Codeigniter : calling a method of one controller from other

I have two controllers a and b.

I would like to call a method of controller a from a method of controller b.

Could anyone help explain how I can achieve this?

This question is related to codeigniter

The answer is


very simple in first controllr call

 $this->load->model('MyController');
 $this->MyController->test();

place file MyController.php to /model patch

MyController.php should be contain

class MyController extends CI_Model {

    function __construct() {
        parent::__construct();
    }
    function test()
    {
        echo 'OK';
    }
}

You can use the redirect URL to controller:

Class Ctrlr1 extends CI_Controller{
public void my_fct1(){
redirect('Ctrlr2 /my_fct2', 'refresh');
}
}


Class Ctrlr2 extends CI_Controller{
public void my_fct2(){
$this->load->view('view1');
}
}

test.php Controller File :

Class Test {
 function demo() {
  echo "Hello";
 }
}

test1.php Controller File :

Class Test1 {
 function demo2() {
  require('test.php');
  $test = new Test();
  $test->demo();
 }
}

You can do like

$result= file_get_contents(site_url('[ADDRESS TO CONTROLLER FUNCTION]'));

Replace [ADDRESS TO CONTROLLER FUNCTION] by the way we use in site_url();

You need to echo output in controller function instead of return.


Very simple way in codeigniter to call a method of one controller to other controller

1. Controller A 
   class A extends CI_Controller {

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

2. Controller B 

   class B extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
    }
    function custom_b()
    {
            require_once(APPPATH.'controllers/a.php'); //include controller
            $aObj = new a();  //create object 
            $aObj->custom_a(); //call function
    }
}

You can use the redirect() function. Like this

class ControllerA extends CI_Controller{
    public function MethodA(){
       redirect("ControllerB/MethodB");
    }
}

Controller to be extended

require_once(PHYSICAL_BASE_URL . 'system/application/controllers/abc.php');

        $report= new onlineAssessmentReport();
        echo ($report->detailView());

I posted a somewhat similar question a while back, but regarding a model on CI.

Returning two separate query results within a model function

Although your question is not exactly the same, I believe the solution follows the same principle: if you're proposing to do what you mention in your question, there may be something wrong in the way you're coding and some refactoring could be in order.

The take home message is that what you're asking is not the way to go when working with MVC.

The best practice is to either use a Model to place reusable functions and call them in a controller that outputs the data through a view -- or even better use helpers or libraries (for functions that may be needed repeatedly).


I agree that the way to do is to redirect to the new controller in usual cases.

I came across a use case where I needed to display the same page to 2 different kind of users (backend user previewing the page of a frontend user) so in my opinion what I needed was genuinely to call the frontend controller from the backend controller.

I solved the problem by making the frontend method static and wrapping it in another method. Hope it helps!

//==========
// Frontend
//==========
function profile()
{
   //Access check

   //Get profile id
   $id = get_user_id();

   return self::_profile($id);
}

static function _profile($id)
{
   $CI = &get_instance();
   //Prepare page
   //Load view
}

//==========
// Backend
//==========
function preview_profile($id)
{
   $this->load->file('controllers/frontend.php', false);

   Frontend::_profile($id);
}