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

#1
[eluser]FinalFrag[/eluser]
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:

Code:
$this->load->view('header');

// view stuff goes here

$this->load->view('footer.php');

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:

Code:
// close some open divs from the content
</div></div></div>

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

// end the page completely
&lt;/body&gt;
&lt;/html&gt;

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...

#2
[eluser]Seppo[/eluser]
A post-controller constructor hook would do it.

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

#4
[eluser]Seppo[/eluser]
Sure.
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
Code:
function myhook()
{
  $CI =& get_instance();
  $CI->load->model('menu');
  $data['menu'] = $CI->menu->getSomeData();
  $CI->load->vars($data);
}

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

#5
[eluser]wiredesignz[/eluser]
Modular Extensions would be a better choice than using hooks. See my signature.

#6
[eluser]zdknudsen[/eluser]
You could also try and put this experimental library of mine in your /application/libraries/ directory and simply call your other controller through
Code:
$this->load->controller('segment1/segment2/etc');

I haven't tested it very much, though.

http://www.filepanda.com/file/5wvzss2zkkp5/

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.

#7
[eluser]Seppo[/eluser]
Aren't those solutions overkill?

#8
[eluser]gtech[/eluser]
can't you just load the model in the constructor of every controller?

#9
[eluser]wiredesignz[/eluser]
lol gtech is right, autoload the model Tongue

#10
[eluser]FinalFrag[/eluser]
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):

Code:
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.