I more or less asked the same questions on stackoverflow.com, but maybe this is more for this forum. I have a hard time getting to grips with how to implement a top navigation (common for the whole site) and a page specific navigation (common for the current controller). I use Colin Williams' Template library.
I have written my own MY_Controller
which all page controllers should extend, but I do not know if this is the correct way. The page specific navigation is implemented in the code below.
MY_Controller.php
abstract class MY_Controller extends CI_Controller {
private $_nav;
public function __construct() {
parent::__construct();
}
abstract protected function _get_nav();
protected function _load_nav(array $nav = null) {
if ($nav === null) {
$this->_nav = $this->_get_nav();
} else {
$this->_nav = $nav;
}
}
protected function _render() {
if (func_num_args() < 3) {
return false;
}
$args = func_get_args();
$this->template->write('title', array_shift($args));
$data = array_pop($args);
for ($i = 0; $i < count($args); $i++) {
$this->template->write_view('content', array_shift($args), $data);
}
if ($this->_nav === null) {
$this->_load_nav();
}
$this->template->write_view('nav', 'templates/sidebar_nav', array('nav' => $this->_nav));
$this->template->render();
return true;
}
}
collection.php
class Collection extends MY_Controller {
public function __construct() {
parent::__construct();
$this->load->model('collection_model');
}
protected function _get_nav() {
return $this->config->item('collection_nav');
}
public function index() {
$this->view();
}
public function view() {
$data = array();
$data['list'] = $this->collection_model->get_list();
$this->_render('View Collection', 'collection/view', $data);
}
}
sidebar_nav.php
<ul class="nav nav-list bs-docs-sidenav">
<?php foreach ($nav as $item) : ?>
<?php $class = isset($item['class']) ? ' class="' . $item['class'] . '"' : ''; ?>
<li<?php echo $class; ?>>
<a href="<?php echo $item['href']; ?>"><?php echo $item['value']; ?></a>
</li>
<?php endforeach; ?>
</ul>
What do you think? Is this the proper way to produce a navigation system?