views:

107

answers:

2

I am solving a kind of architectural problem inside CI. I need to be able to instantiate other controllers and their methods in main controller. E.g.

*main.php/function index():*
$controller2 = new Controller2();
$data['pre_loaded_data'] = $controller2 ->ajax_get_some_view(array('static'=>true));

The goal behind this approach is to build ajax application which loads some screen parts statically at first load, as part of main html page, but later these parts are updated with ajax methods to from various other controllers(at this time with array('static'=>false) param), e.g as reponse to onclick event on main page.

The problem is that CI doens't seem to be design to support multiple controllers and throws various loader-related errors reporting that some class is not loaded even when it's loaded.

What would be the best approach to pre-load data from other controllers in the main controller?

+2  A: 

You might want to make base classes that controllers will inherit. You can also consider trying with HMVC by using Modular Extensions.

Here's a great read on base classes by Phil Sturgeon and how to implement them.

rkj
+1  A: 

Try putting functions like get_some_data() on a library, then you'll be able to include and call them from every controller and every function without problems, sending the results formatted in a partial view (static case) or back to the browser after an ajax call.

Always remember to deal with data only on models and libraries. This way you'll not have to worry about conficting controllers.

// main/index
$this->load->library('your_library');
$data['foo'] = $this->your_library->get_foo();

// controller2/get_some_ajax_data
$this->load->library('your_library');
echo $this->your_library->get_foo() || '';
gpasci