• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Calling 2 controllers?

I'm currently working on my website and I got the following problem:

I have a header.php that is included at the top of every view.
I also have a footer.php that is included at the bottom of every view.
So basicly it comes down to this in my view files:


// view stuff goes here


My menu is in the footer.php file because it needs to be parsed after the content does. So the footer.php file looks like this:

// close some open divs from the content

// include the menu
require_once 'menu.php';

// end the page completely

The problem is as follows:
In this menu I need information from the database. However, when I use $this->load->model('menu') it doesn't work (figures, as I am in a view, not a controller).

How can I solve this? Should I make a call to another controller (if that is at all possible) or am I doing something else wrong.

Thnx in advance...

A post-controller constructor hook would do it.

Could you give me a bit more info/how-to on that subject? I'm still pretty new to CodeIgniter...

To set up a hook, take a look here, it's quite simple.
Then you should have a function that will be called on every request, something like this
function myhook()
  $CI =& get_instance();
  $data['menu'] = $CI->menu->getSomeData();

Then, on any view loaded you will have $menu set with the result from the controller.

Modular Extensions would be a better choice than using hooks. See my signature.

You could also try and put this experimental library of mine in your /application/libraries/ directory and simply call your other controller through

I haven't tested it very much, though.


Edit: Actually, you should be able to work out your problem without any fancy workarounds. First of all, why are you require_once-ing your menu.php? What kind of file is it? And why does the menu need to be parsed after the content? Usually you would probably use either a model or library to parse your menu and save it to a variable that you pass to the view.

Aren't those solutions overkill?

can't you just load the model in the constructor of every controller?

lol gtech is right, autoload the model Tongue

Autoloading it every time is a bit to much work. Cause I just know I will forget some day :p

Currently I solved it like this (menu.php):

if (!defined('BASEPATH')) exit('No direct script access allowed');

$query = mysql_query("SELECT title, text FROM ff_extra WHERE status = 1 ORDER BY sequence ASC");
if (mysql_num_rows($query) > 0 ) {
    while (list($title, $text) = mysql_fetch_array($query)) {
        echo '<h1>' . $title . '</h1>';
        echo '<p>' . $text . '</p>';
else {
    echo '<h1>No info</h1>';
    echo '<p>There is no extra information available at the moment.</p>';

I'm looking for the 'good' way to do something like this, but it shouldn't be more code than this, because that would be kind of stupid

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.