问题
I am a newbie with codeigniter. I am trying to write an application using mysql database. In my site I want to use menu as :
+Homepage
+About
+Services
+Education services
+neurofeedback
+biofeedback
I need some information to understand. I use pages controller as main pages controller:
<?php
class Pages extends CI_Controller {
public function view($page = 'home')
{$this->load->view('templates/header', $data);
$this->load->view('pages/'.$page, $data);
$this->load->view('templates/footer', $data);
my questions are :
1) where the menu controller must be coded inside pages controller or seperate one?
2) how can i make the menu controller from database ?
3) How can i make relation with the menu id and the page id?
I made lots of research but i need a little bit more understanding .
Thank you for your help.
Edit : I have used MY_Controller as you say .
This is my pages controller :
class Home extends MY_Controller {
function __construct() {
parent::__construct();
}
public function view($page = 'home')
{
$this->load->helper('text');
$data['records']= $this->services_model->getAll();
if ( ! file_exists('application/views/pages/'.$page.'.php'))
{
// Whoops, we don't have a page for that!
show_404();
}
$data['title'] = ucfirst($page); // Capitalize the first letter
$this->load->view('pages/'.$page, $data);
}
}
回答1:
where the menu controller must be coded inside pages controller or seperate one?
Assuming that you have a template that must be followed by all pages, I suggest you to do this.
1. Create a basic controller
In the ./application/core/ folder, create a file called MY_Controller
class MY_Controller extends CI_Controller {
protected $data = array();
function __construct() {
parent::__construct();
}
function render_page($view) {
//do this to don't repeat in all controllers...
$this->load->view('templates/header', $this->data);
//menu_data must contain the structure of the menu...
//you can populate it from database or helper
$this->load->view('templates/menu', $menu_data);
$this->load->view($view, $this->data);
$this->load->view('templates/footer', $this->data);
}
}
2. Create one controller to each page and use MY_Controller instead of CI_Controller
class Homepage extends MY_Controller {
function __construct() {
parent::__construct();
}
function index() {
//define data that the view can access
$this->data['someDataToView'] = 'Some data';
$this->render_page('pages/homepage');
}
}
how can i make the menu controller from database?
Well, you will not have a controller for the menu, but a view instead.
Possibilities to the menu
- Create a view for the menu, load the records from the database in MY_Controller, load the view in
render_page()
; - Create a view for the menu, create a Helper function that defines the menu structure and use in MY_Controller, load the view in
render_page()
;
Example of menu template (adjust for your scenario):
./application/views/templates/menu.php
<ul>
<?php foreach($menus as $menu): ?>
<li><a href='<?php print $menu["url"] ?>'><?php print $menu["title"] ?></a></li>
<?php endforeach; ?>
</ul>
edit
Given your Home controller, I think the error is in your file_exists check. See the Home controller that I change:
class Home extends MY_Controller {
function __construct() {
parent::__construct();
}
public function view($page = 'home') {
$this->load->helper('text');
//always use $this->data
$this->data['records']= $this->services_model->getAll();
if ( ! file_exists(APPPATH.'views/pages/'.$page.'.php'))
{
//check the content of APPPATH.'views/pages/'.$page.'.php'
// Whoops, we don't have a page for that!
show_404();
}
$this->data['title'] = ucfirst($page); // Capitalize the first letter
//if you use the MY_Controller, check the render_page function...
//$this->load->view('pages/'.$page, $data);
$this->render_page('pages/'.$page)
}
}
来源:https://stackoverflow.com/questions/11751624/codeigniter-dynamic-navigation