I am working on a fairly large CodeIgniter project. There will be more pages than I can count. Right now, I have a few different headers, depending on what page the user is visiting and also depending on if the user is logged in. For example, if the user is not logged in (no session variable stored) then I want to display header1 which shows some basic stuff like "Signup, Login". If the user IS already logged in, I want to show different menu items like "logout". That's a very basic example. The changes can be pretty extensive, so it wouldn't make sense for me to have one header file with control flow logic in it.
I'm looking for a way to include the appropriate header without having to write $this->load->view('header')
in every method I have in my controllers. It looks like another option is to write that line in every view that I have. To make this dynamic I created a MY_Controller
file and included something like this in it:
public function get_header()
{
if ($this->session->userdata('user_id')) {
$this->load->view('headers/logged-header');
} else {
$this->load->view('headers/header-home');
}
}
Then I include <?php MY_CONTROLLER::get_header(); ?>
in places that I need it (i.e inside methods or views.)
Is there a way to "hook" this in somehow, where I can simply write this code once, and then the application process it every time it loads one of my methods that call up a page?