CodeIgniter Forums
ControllerA calls method from ControllerB - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: ControllerA calls method from ControllerB (/showthread.php?tid=26727)



ControllerA calls method from ControllerB - El Forum - 01-21-2010

[eluser]tokyotech[/eluser]
How do I get one controller to call a method from another controller? Is it considered bad practice if I have to resort to doing this? Let me explain why I need to do this. I have this ControllerB which has a methodB() which I'd like to use both from JS land and PHP land.

Code:
class ControllerB extends Controller {
   public function methodB() {
      session_start();
      $_SESSION['sessionB'] = 'i am set';
   }
}

JS can call it through AJAX:

Code:
[removed]
   new Ajax.Request(
      'index.php/controllerB/methodB'
   );
[removed]

But it's also useful in PHP land for controllerA to call controllerB/methodB:

Code:
class ControllerA extends Controller {
   public function methodA() {
      //psuedocode which I don't know how to do:
      $this->load->controller('controllerB');
      $this->controllerB->methodB();
   }
}



ControllerA calls method from ControllerB - El Forum - 01-21-2010

[eluser]Colin Williams[/eluser]
That's just an architecture that doesn't make sense to me. Any bit of code that two controllers must execute should live in either a model, a library, or available in the parent class (Controller or MY_Controller)