[eluser]Barber of Padua[/eluser]
To avoid repeating code I want the site navigation defined just once, not in all methods of a controller. Is this solution ok, or ... suggestions? (newbie)
first in application/libraries the Controller gets extended and gets a property $navigation
Model gets autoloaded
Code:
class MY_Controller extends Controller {
var $navigation;
function MY_Controller(){
parent::Controller();
$this->getMainNav('globalmenu');
}
function getMainNav($menu){
$this->navigation = $this->Pages_model->getNav($menu);
}
}
then in the 'regular' controller
Code:
class Page extends MY_Controller {
function Page()
{
parent::MY_Controller();
}
function index()
{
$data = array();
$data['main_nav'] = $this->navigation;
$data['content'] = $this->Pages_model->getPageData('home');
$this->load->vars($data);
$this->load->view('main_layout');
}
function contact()
{
$data = array();
$data['main_nav'] = $this->navigation;
$data['content'] = $this->Pages_model->getPageData('contact');
//etc
}
}
Still got some code repeated, though