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
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.
(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.
Let all your controllers (which are classes) extend the MY_Controller's class. Like that, you can use "common" methods in all your controllers.
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)!
(06-20-2019, 12:47 PM)InsiteFX Wrote: [ -> ]If you use the HMVC it allows controllers to load other controllers.
codeigniter-modular-extensions-hmvc
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!
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
//$object is the oject of my controller
class Front {
public function __construct() {
//Do what you want
}
public function footer($object) {
$object->load->view('template/footer',$object->data);
}
public function header($object) {
$object->data['header'] = 'My header';
$object->load->view('template/header',$object->data);
}
public function page($object) {
//Example: you can load everything with $object
$object->load->helper('url_helper');
$this->header($object);
$this->footer($object);
}
}
My controller
PHP Code:
include_once (__DIR__.'/../Front_controller/Front.php');
class Com extends CI_Controller {
public $data;
public function __construct() {
parent::__construct();
}
public function index() {
$this->data['footer'] = 'My footer';
$front = new Front();
$front->page($this);
}
}
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.