CodeIgniter Forums

Full Version: Multiple controllers on 1 page
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]Unknown[/eluser]
Hi everybody,

I just started out with CI, and i couldn't seem to find if and (maybe) how i can use multiple controllers in 1 controller.

This may sound a bit weird, so let me explain to you what i want.

I have one main template, that i always want to load.
This contains a Menu, a content area and a sidebar.

What i would like to do/have is:
Have 1 controller that loads the menu and content area.
Have 1 controller that loads the sidebar.

The menu and content area have thing in common, like page title and such, and therefor i want them in 1 controller.
But the sidebar is always the same, and needs a bit of coding.
Now i'm trying to keep the sidebar code out of my controller, so i wouldn't have to copy-paste it all the time.

Is this possible? and if so, How can i achieve this?

Kind Regards,
Nico Kaag

El Forum

[eluser]brucebat[/eluser]
That seems a bit over complicated.

What you should have is a template view which loads header, navigation, sidebar, and footer and has a variable called $content

This $content will contain the value of your filler for that page e.g.

Code:
function index()
{
$content= 'home';
$this->load->view('template', $content);
}

This would then pass that variable $content to the template view which would then load up that page like so within itself

$this->load->view ($content);

El Forum

[eluser]WanWizard[/eluser]
Not too complicated, if you think in widgets and loose coupling.

I use modules, where module controller method produce html (via module views) that generate a widget. The local in the main app controller inserts those in a template.

You can do that in CI, but you'll need a modular solution, as native CI doesn't support controllers calling controllers, which makes it a bit more complicated.

You can use @brucebat's suggestion too, but given the fact that widgets can appear on any page, and in different locations, you'll have to repeat a lot of code, which goes against DRY.

El Forum

[eluser]toopay[/eluser]
[quote author="WanWizard" date="1309491504"]
You can do that in CI, but you'll need a modular solution, as native CI doesn't support controllers calling controllers...[/quote]

...or, as an alternative, you can use some proxy technique to call and get the result of other controller from some controller.

El Forum

[eluser]Unknown[/eluser]
Currently i'm using brucebat's solution, but that does give me like 40 lines of code in each controller that i want to avoid.

The idea is indeed widget based.
I'm thinking of maybe using libraries or something to solve it, but can i load a view within a library?
Than every library will have some kind of "getHTML()" method, witch returns the view string.

@WanWizard, Do you have any type of example code on how you're doing this? Would be a great help to me! =รพ

El Forum

[eluser]toopay[/eluser]
Play with Proxy Library, on my signature link and see what you can do with that.

But if you works with large application and complex logic on your controller, then Wan suggestion, Modular or HMVC way, will be the right option for that.

El Forum

[eluser]CodeIgniteMe[/eluser]
You can do your approach through the use of the other two (Model and View)
1. Separate the header, menu, content, sidebar, footer, etc into their own view files.
2. Create a model that is dedicated to process things like log-in status verification, retrieving user data (such as the username or email address) from database or session.
3. Create a method in the model class that returns the processed view to the controller/caller


And by the way, is this code valid? Can I consider this as calling another controller from my controller?
Code:
class Myclass extends CI_Controller
{
$cont2 = null;
function __construct()
{
parent::__construct();
require_once('path/to/another/controller/mycontroller.php');
$this->cont2 = new Mycontroller();
}

function index()
{
$this->cont2->do_stuff();
}

}

El Forum

[eluser]toopay[/eluser]
[quote author="OOP-MVC addict" date="1309530858"]And by the way, is this code valid? Can I consider this as calling another controller from my controller?
Code:
class Myclass extends CI_Controller
{
$cont2 = null;
function __construct()
{
parent::__construct();
require_once('path/to/another/controller/mycontroller.php');
$this->cont2 = new Mycontroller();
}

function index()
{
$this->cont2->do_stuff();
}

}
[/quote]
As Wan already stated, CI doesn't support native HMVC way, and despite the fact that not everyone did like "dirty" line, like loading files/class and build a new object, in every controller just to call another function inside another controller, that approach may works.

El Forum

[eluser]wiredesignz[/eluser]
You can use Modular Extensions HMVC or the Widget plugin. See my signature for links to both solutions.