你真的喜欢复制/粘贴许多$ this-> load-> view()到任何控制器函数吗? 这是一个意大利面条的代码。您可以尝试下一步:例如,我们将main.php控制器作为默认控制器。这个主控制器包含主要功能:
public function index()
{
ob_start();
$this->load->model('mainmodel');
$data = $this->mainmodel->_build_blocks(); //return array with needed blocks (header, menu, content, footer) in correct order
foreach ($data->result_array() as $row) {
$this->load->module($row['block_name']);
$this->name = new $row['block_name'];
$this->name->index();
}
ob_end_flush();
}
所以,互相控制器还具有可以分派动作取决于URL段,准备PARAMS等index()函数
页脚控制器如实施例(I使用的Smarty作为模板引擎):
public function index()
{
$this->mysmarty->assign('year', date("Y"));
$this->mysmarty->view('footer');
return true;
}
内容控制器将有:
public function index()
{
$name = $this->uri->segment(1, 'index');
$act = $this->uri->segment(2, 'index');
$this->load->module($name);
$this->name = new $name;
$pageData = $this->name->_show($act);
if ($pageData)
{
$this->mysmarty->assign($name, $pageData);
}
$this->mysmarty->view($name);
}
米
那意味着什么,如果你想显示http://site.name/page/contactus,我们下一步:
1)main.php通过需要块
2)首先,我们通过头控制器显示header.tpl启动周期
3 ),那么我们显示菜单
4)则称其解析URL内容控制器,发现他应该叫什么_show()在页面控制器功能,并通过行动=“联系我们”给它。 _show()函数可以包含一些开关/壳体结构,其示出的模板依赖动作名称(在此情况下contactus.tpl)
5)最后,我们看看页脚模板
在这种情况下,我们有柔性结构。所有的控制器应具有指数()函数和所有的控制器谁能够在内容被称为应有_show($行为)功能。就这样。