首先
$this->load->view('filename');
可以在同一个控制器函数中多次使用,并且它们会以正确的顺序出现。
您所做的是分开的控制器部分必须在同一个控制器中使用。我想你有这些文件像
/application/controllers/static_page.php
/application/controllers/user.php
/application/controllers/system.php
而且,如果不对 CodeIgniter 进行大量修改,您就无法从另一个控制器调用一个控制器,而这正是阻止您的原因。如果您必须为许多功能使用相同的处理代码,您必须创建一个helper,您可以在任何地方使用,或者更优雅的解决方案,即在 /libraries/MY_Controller.php 中具有通用控制器功能。
MY_Controller.php 总是在控制器被调用之前被调用
<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
class MY_Controller extends CI_Controller
{
function __construct()
{
parent::__construct();
// you can do any kind of processing here
// in example if you have some view counter
// this place is always called
}
function a_generic_function($rawdata)
{
// do some processing here
return $data;
}
}
然后,您将能够在您的任何控制器中使用此功能:
$this->a_generic_function($rawdata);
现在,处理页眉和页脚最聪明的方法是制作一个包含页眉和页脚的“布局”文件。
这是 layout.php 视图的示例:
<!doctype html>
<head>
<meta charset="utf-8">
<title><?php echo $title; ?></title>
</head>
<body>
<div id="container">
<header>This page has been made by <?php echo $user_name; ?>/header>
<div id="main" role="main">
<?php echo $main_content_view; ?>
</div>
<footer>Write your footer here</footer>
</div>
</body>
</html>
你看到中间有 $main_content_view。
您将在控制器中执行此操作:
// add the HTML for the login to the variable
// TRUE means we don't output it in the browser, but put it in a String
$this->viewdata["main_content_view"] = $this->load->view('login', $data, TRUE);
// want to add more data? just ADD it to the variable, like a String
$this->viewdata["main_content_view"] += $this->load->view('main', $data, TRUE);
// done? send it to the layout, and pass it the $this->viewdata
$this->load->view('layout', $this->viewdata);
你还能用这个做什么?您可以像这样使用 $this->viewdata:
$this->viewdata["title"] = 'Function title';
$this->viewdata["user_name"] = $user_name;
因此您可以像任何其他视图一样编辑您的布局。