CodeIgniter Forums
A controller function can load another controllers?? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: A controller function can load another controllers?? (/showthread.php?tid=35296)



A controller function can load another controllers?? - El Forum - 10-25-2010

[eluser]atoleon[/eluser]
Hi

Can i load a controller like i load a view?


A controller function can load another controllers?? - El Forum - 10-25-2010

[eluser]WanWizard[/eluser]
In standard CI, no, you can't.

You can with a Modular solution, like Modular CI.


A controller function can load another controllers?? - El Forum - 10-25-2010

[eluser]ShoKatoo[/eluser]
[quote author="atoleon" date="1288037498"]Hi

Can i load a controller like i load a view?[/quote]

Please give an example... Is it something like calling a function declared in controller A from controller B?
If that is so you can do this:

Controller A

Code:
<?php
class Conta extends Controller {
    function Conta(){
        parent::Controller();
    }
    function index(){
    }
    function test_function(){
        echo("It's working!!!");
    }
}
/* End of file conta.php */
/* Location: ./application/controllers/ */
Controller B
Code:
<?php
require_once("conta.php");
class Contb extends ContA{

    function Contb(){
        parent::Conta();
    }

    function index(){
        
    }
}
/* End of file contb.php */
/* Location: ./application/controllers/ */
It works fine and I belive that it is the best practice.
You can now call function test_function() like http://localhost/contb/test_function even if you have no test_function() in Contb controller. The Contb controller extends the Conta controller inheriting all its function/methods.

Have Phun! Smile


A controller function can load another controllers?? - El Forum - 10-25-2010

[eluser]WanWizard[/eluser]
That's not one controller calling another, that is extending a class. Totally different concept.


A controller function can load another controllers?? - El Forum - 10-25-2010

[eluser]ShoKatoo[/eluser]
[quote author="ShoKatoo" date="1288045763"][...] The Contb controller extends the Conta controller inheriting all its function/methods.[...][/quote]
Exactly what I said Tongue


A controller function can load another controllers?? - El Forum - 10-25-2010

[eluser]tonanbarbarian[/eluser]
if you find you have code in one controller that you would like to access from another controller then you probably need to move that common code into a library and then call the library from each controller