Welcome Guest, Not a member yet? Register   Sign In
Using sessions and setting variables in pre_controller hook
#1

[eluser]FinalFrag[/eluser]
I have a website that has a menu at the top. The items of this menu are in a database. I could load a model in all of my controllers, but I rather have a piece of common code somewhere so that I don't have to think about loading the model and calling the getMenuItems() function myself.

From the userguide, the only possible solution I see are hooks, a pre_controller one to be exact.

I've been fooling around with this, but I'm stuck with 2 problems:
1) How do I pass the session to a pre_controller hook
2) How do I set variables that I can use later on

I need to have access to the session because the menu when you are not logged in is different from the one you get when you are.

The need to set a variable is to call the $header_menu variable in my view.

Thnx in advance
#2

[eluser]Phil Sturgeon[/eluser]
I used to try doing this with pre_controller hooks but came across a better way posted here by a user I forget.

The exact same effect can be done via extending the controller class. Its a little less beautiful but it works a treat.

Make MY_Controller.php and inside it do code such as:

Code:
<?php
class MY_Controller extends Controller {

var $data; // This will be used instead of $data in to be passed to your views

function MY_Controller {
  parent::Controller();

  $this->data->menu = 'foo';
}

}
?>

Then this $this->data->menu work throughout your controller and can be passed to views as well.
#3

[eluser]Phil Sturgeon[/eluser]
Not the post I was talking about, but this one explains it:

http://ellislab.com/forums/viewthread/89875/




Theme © iAndrew 2016 - Forum software by © MyBB