This is my first attempt with MVC and I almost get it, but a small thing bothers me.
I have this controller in CodeIgniter:
<?php
class Page extends CI_Controller {
public function index($page = "home")
{
$data['page'] = $page;
list($data['title']) = array_keys($this->_menu(), $page);
$data['menuItems'] = $this->_menu();
$this->load->view('templates/header', $data);
$this->load->view('templates/menu', $data);
$this->load->view('pages/'.$page, $data);
$this->load->view('templates/footer', $data);
}
private function _menu()
{
static $menuItems = array(
"Home page" => "home",
"Our history" => "history",
"About us" => "about",
"Contact page" => "contact"
);
return $menuItems;
}
}
And here is the menu view:
<?php
$menu = "";
foreach ($menuItems as $menuName => $view) {
$menu .= '<li';
if ( $page == $view ) {
$menu .= ' class="selected">';
$menu .= '<a href="#">';
} else {
$menu .= '><a href="' . $view . '">';
}
$menu .= $menuName . '</a></li>' .PHP_EOL ;
}
echo $menu;
Where do the menu items belong in MVC logic? Is it ok to store things like the menu in the Controller
or do I have to make a new Model
for it like this?
class Menu extends CI_Model {
public function __construct()
{
parent::__construct();
}
public function get_menu_items()
{
static $menuItems = array(
"Home page" => "home",
"Our history" => "history",
"About us" => "about",
"Contact page" => "contact"
);
return $menuItems;
}
}
Then load it from Controller
:
public function index($page = "home")
{
...
$this->load->model('Menu');
$data['menuItems'] = $this->Menu->get_menu_items();
$this->load->view(...);
$this->load->view(...);
$this->load->view(...);
}