I'm working on a site where there can be up to 3 levels of hierarchy in the navigation.
- main sections in the top header
- sub-navigation on the left
- optional sub-sub navigation below that
In the past, when I've rolled my own with PHP, I would create a file navigation.php holding a class and arrays for all the sections and sub sections and a couple of functions to output the navigation. I'd set variables on every page in the site (current_section ='', current_sub_section='') so the navigation function would know what to highlight.
In CodeIgniter, I'm wondering if this is still a good approach to use?
I'm going to assume that your main nav sections almost directly map to your controllers, ie.
Home | News | Events
In your top bar, maps to:
If this is the case, you already have a simple way of selecting your nav array.
You can put an array of nav item-link pairs in your controller constructor & pass them along to a sub-view in your output.
Example:
class HomeController extends CI_Controller
{
private $nav;
public function __construct()
{
parent::__construct();
$this->nav = array(
array('Browse', site_url('news/browse')),
array('Edit', site_url('news/edit'))
);
$this->load->vars(array('NavigationArray' => $this->nav));
}
// ...
}
Now what you've done is auto-registered a variable in all your views $NavigationArray
that contains an array of Display Name - Link pairs.
You can then load a basic Navigation View that builds up your subnav from that variable (as it's availiable everywhere).
<? foreach($NavigationArray as $entry): ?>
<a href="<?=$entry[1];?>"><?=$entry[0];?></a>
<? endforeach; ?>
And below that you can look for the existance of a sub-nav array that you could optionally set in your controller, or whatever (the third optional nav you talked about)
<? if(exists($SubNavigationArray)): ?>
<? foreach($SubNavigationArray as $entry): ?>
<a href="<?=$entry[1];?>"><?=$entry[0];?></a>
<? endforeach; ?>
<? endif; ?>
Please remember, this example is very basic, but this is generally how we pass data around, I wouldn't want you to put global variables anywhere and try to queue off them. Simply "load" variables into the view engine, and they'll be availiable when you go to render your views/subviews.
This way the controller controls what nav items get displayed.
Also note:
You can pass the variables explicitly instead of trusting they will exist in the scope of your view.
$this->load->view('myview', array('NavigationArray' => $this->nav));
Hope this helps.
Here's basically what I do to determine "active" links in Codeigniter:
$active_class = '';
$url = site_url('your/link/url');
if ($url == current_url())
{
$active_class = ' class="active"';
}
$link = '<a href="'.$url.'"'.$active_class.'>Link Text</a>";
Keep in mind this is a basic example and usually run in a loop. The best way depends on what your navigation array looks like, and what you consider to be "active" (f you want to "activate" links whose href partially match the url).
The best way is to use 3 different views, a top view, a left view and a bottom view then in your controller you can pass the apporiate vars to each view so you would do something like
$top[current] = something;
$top[current_sub] = somethingelse;
$this->load->view('top_nav', $top);
$this->load->view('left_nav',$left);
...
Then in your views you can handle the variables passed to them.