CodeIgniter Forums
Sharing a feature in more than one controller - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Sharing a feature in more than one controller (/showthread.php?tid=62787)



Sharing a feature in more than one controller - groovebird - 08-26-2015

I have 3 controllers for different pages of product types.
In all this pages only the header area looks similar (images and text form a database, some price calculations). I don't want to create this area in all 3 controllers over and over again.
How can i share this feature in all 3 controllers? In the MY_Controller it is always loaded (not only on product pages) and i think this is not so good. Is there another solution or best practise?


RE: Sharing a feature in more than one controller - orionstar - 08-26-2015

You can create multiple base controllers to achieve this. You should create a Products_base controller in the core directory then you should include that file at the beginning of every products related controller and extend that controller instead of CI_Controller or MY_Controller...
If you don't want to include that every time then you should create an spl_autoload method maybe in the front-controller (index.php in the root)!


RE: Sharing a feature in more than one controller - groovebird - 08-26-2015

Thank you. This works fine. I havn't done so much with CI and sometimes don't know what keywords to use for searching. But this forum is great.


RE: Sharing a feature in more than one controller - albertleao - 08-26-2015

Depending on how broad the feature is, I like to create a library and autoload the library. You can access the library in any controller that way.