Codeigniter动态导航 [英] Codeigniter dynamic navigation
问题描述
我是Codeigniter的新手.我正在尝试使用mysql数据库编写应用程序. 在我的网站上,我想将菜单用作:
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);
我的问题是:
1)菜单控制器必须在页面控制器内编码还是单独编码?
1) where the menu controller must be coded inside pages controller or seperate one?
2)我如何从数据库中制作菜单控制器?
2) how can i make the menu controller from database ?
3)如何与菜单ID和页面ID建立联系?
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 .
谢谢您的帮助.
正如您所说,我已经使用了MY_Controller.
Edit : I have used MY_Controller as you say .
这是我的页面控制器:
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);
}
}
推荐答案
菜单控制器必须在页面控制器内部编码还是单独编码?
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.创建一个基本控制器
在./application/core/文件夹中,创建一个名为MY_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.在每个页面上创建一个控制器,并使用MY_Controller代替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.
菜单的可能性
- 为菜单创建一个视图,在MY_Controller中从数据库中加载记录,在
render_page()
中加载该视图; - 创建菜单视图,创建 Helper 函数,该函数定义菜单结构并在MY_Controller中使用,将视图加载到
render_page()
;
- 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()
;
菜单模板示例(根据您的情况进行调整):
./application/views/templates/menu.php
./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>
修改
鉴于您的Home控制器,我认为该错误在于您的file_exists检查中.请参阅我更改的Home控制器:
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)
}
}
这篇关于Codeigniter动态导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!