Load controller from another controller |
Hello,
I was wondering if it's possible to load a controller through another controller. As i've searched the internet i found some articles that people mentioned that this is "not a working practice for MVC pattern". The reason i want to achieve such a thing is for situations like: e.g.: Let's say i want to get something from the db and place in header or footer (analytics scripts, footer copyrights text etc..) and in order to have cleaner "view" files i want to create some controllers (sth like "common/header", "common/footer" etc.) and be able to load them in the other controllers. Any ideas / suggestions on that? Thanks //Life motto if (sad() == true) { sad().stop(); develop(); }
Did you think of using a library for that? Or create a MY_Controller.php in the application/core folder. Write a method to generate a common set of views or even different sets of views.
Let all your controllers (which are classes) extend the MY_Controller's class. Like that, you can use "common" methods in all your controllers.
If you use the HMVC it allows controllers to load other controllers.
codeigniter-modular-extensions-hmvc What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
(06-20-2019, 12:46 PM)Wouter60 Wrote: Did you think of using a library for that? Or create a MY_Controller.php in the application/core folder. Write a method to generate a common set of views or even different sets of views. I've tried this method and yeas it works! But i see a lot of people suggest not to do such thing like extending controllers in core folder. That's why i thought to ask for an alternative (in case there is one)! //Life motto if (sad() == true) { sad().stop(); develop(); } (06-20-2019, 12:47 PM)InsiteFX Wrote: If you use the HMVC it allows controllers to load other controllers. I've seen it and i'll use it as it looks like, but i was wondering if there was any other alternatives! Thanks though for pointing the right direction! //Life motto if (sad() == true) { sad().stop(); develop(); } Quote:But i see a lot of people suggest not to do such thing like extending controllers in core folder. These people are probably trying to keep you away from modifying the system/core files. But the application/core folder isn't forbidden ground if you want to use a MY_Controller or MY_Model etc.
WiredDesigns needs to be patched to work with current codeigniter installs. This is the skeleton I created for myself that I use:
https://github.com/mladoux/ci-skeleton It has HVMC and a custom autoloader mod so you can name your controller extensions whatever you want ( not just MY_Controller ).
You can do that (but I think library and real MY_controller are better):
I Have a directory 'application/font_controller' with a file called Font Front.php PHP Code: <?php PHP Code: include_once (__DIR__.'/../Front_controller/Front.php');
The correct way to do this is to have a global Model being loaded by autoload, and there you create a function that will perform the insertion of your header and footer, you can also send a parameter and use as embedding views.
public function loadLayout($view = null, $data) { //Load your data here $this->load->view('header', $data); $this->load->view($view, $data); $this->load->view('footer', $data); }
Use a library or a model.
Codeigniter is simply one of the tools you need to learn to be a successful developer. Always add more tools to your coding arsenal!
|
Welcome Guest, Not a member yet? Register Sign In |