CodeIgniter Forums

Full Version: Load default views
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]Wogan[/eluser]
My site has two views (global_header and global_footer) that are static, and are supposed to be called for every controller - at the beginning and end respectively.

Loading global_header is easy - $this->load->view('global_header') in the __construct() function. The problem is loading global_footer after the code in any one method is executed.

Is there some sort of "global end" hook (or something) that can be defined in one place in every controller, and not in every individual method?

El Forum

[eluser]bgougent[/eluser]
Dear Wogan,

maybe you need a look at this post
multiple views per controller

Normal I append my code to an array and put them to a function in my controller or model that will combine serveral views depending the params passing to the function.

El Forum

[eluser]Wogan[/eluser]
Thanks, but after using the word "hook", I thought there might be an easier way. Turns out there was.

In config/config.php, set $config[enable_hooks] = TRUE (line 91). Then in config/hooks.php, I added:

Code:
$hook['post_controller'] = array(
'class'    => 'Customhooks',
'function' => 'global_footer',
'filename' => 'Customhooks.php',
'filepath' => 'hooks',
'params'   => ''
);

Then in application/hooks, I created Customhooks.php, with the following code:

Code:
class Customhooks extends Controller
{

    function global_footer()
    {
        $this->load->view('global_footer');
    }

}

And bingo. It worked Smile

El Forum

[eluser]Colin Williams[/eluser]
You could also hook into the Output class from your Controller. Read the sections on Controllers and the Output class to see how this could be done.

El Forum

[eluser]Wogan[/eluser]
It could be done, but it still relies on including extra code along with every controller - something I wanted to circumvent.

My solution could be a problem when handling posted forms/redirects, but I won't know until I get there...

El Forum

[eluser]Colin Williams[/eluser]
The User Guide desperately needs a topic on "Doing Things Globally with Parent Controllers" Search the forums for "MY_Controller" and you might find some answers.

El Forum

[eluser]griffithtp[/eluser]
[quote author="Wogan" date="1227060956"]Thanks, but after using the word "hook", I thought there might be an easier way. Turns out there was.

In config/config.php, set $config[enable_hooks] = TRUE (line 91). Then in config/hooks.php, I added:

Code:
$hook['post_controller'] = array(
'class'    => 'Customhooks',
'function' => 'global_footer',
'filename' => 'Customhooks.php',
'filepath' => 'hooks',
'params'   => ''
);

Then in application/hooks, I created Customhooks.php, with the following code:

Code:
class Customhooks extends Controller
{

    function global_footer()
    {
        $this->load->view('global_footer');
    }

}

And bingo. It worked Smile[/quote]
This is great and works!
HOWEVER, it doesn't load libraries anymore.
Code:
$this->session->userdata('logged_user');
or
Code:
$this->load->library('userlib');
$this->userlib->getdetails();
If userlib is loaded in the autoload, it then works. BUT session is also loaded in autoload.php and still doesn't recognise $this->session as it seems it isn't loaded.
Code:
$this->session->userdata('logged_user'); //<--- this is called in the header hook..