I'm creating a simple application in CI using sessions.
The nav menu is loaded in template.php file.
I want to know what is the best way to make a dinamic menu based in $this->session-userdata('logued').
If is not logued I want to show only "Log in" option in menu, but if is logued I want to load the complete menu options.
What is the best way in CI to do it?
Thanks.
You have the solution right there:
"If is not logued I want to show only "Log in" option in menu, but if is logued I want to load the complete menu options."
Just convert this to a PHP code and you're good to go. There's nothing special CodeIgniter can do for you in this case (I assume you can create separate menus as CI views, but that's about it)... Just check the session and show the appropriate menu based on whether the user is logged in:
View:
if ( $this->session->userdata('logued') ) {
// show real menu
}
else {
// show "log in" menu
}
UPDATE
If you want to do this in your controller, there are many ways as well. For example, you can load different views (if that's your structure):
Controller:
if ( $this->session->userdata('logued') ) {
$menu = 'full_menu'; // or whatever the view name is
}
else {
$menu = 'log_in_menu';
}
// your views, for example
$this->load->view('header');
$this->load->view($menu);
$this->load->view('content');
// etc
Or you can use $data['menu'] = 'full_menu';
and then pass it to the header view (or the only/template view): $this->load->view('header', $data);
...etc. Plenty of options to do this.