正如肖恩所说,如果功能是在控制器级别,你应该扩展CI_Controller。 可能某些功能应该处于视图级别,因此可以使用加载程序视图来帮助您保持每个页面的全局布局并包含常用功能。这是这样的:
[观点] layout.php中:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="es" lang="es">
<head>
<?php
$this->load->view('meta', $data);
?>
</head>
<body>
<div id="wrapper">
<?php
$this->load->view('header', $data);
?>
<div id="contents">
<?php include ('menu_izq.php'); ?>
<div id="page">
<?php $this->load->view($page, $data); ?>
<div class="clear"></div>
</div>
</div>
<div style="clear: both;"></div>
<?php $this->load->view('footer');?>
</div>
</body>
</html>
在你的控制器,你应该总是加载这个视图和参数数组中传递内容的真实观点的价值,就像
$data['page'] = 'incidents'; // this is the real contents
$stylesheets[] = '/scripts/jscalendar-1.0/skins/aqua/theme.css';
$data['stylesheets'] = $stylesheets;
$scripts[] = '/scripts/jscalendar-1.0/calendar.js';
$scripts[] = 'https://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js';
$scripts[] = '/scripts/autoNumeric-1.4.1.js';
$scripts[] = '/scripts/autoLoader.js';
$data['scripts'] = $scripts;
$this->load->view('container',$data);
感谢您的评论。我一直这样做,但似乎并不正确。我最终将得到一个巨大的New_Controller文件,来自各处的代码都混在一起,并从其他控制器类继承。似乎很笨拙。 – noinstance 2011-02-17 16:59:58
@nosuchnick - 没有规则说你应该只有*一个*这样的控制器 - 你可以有一个`Base_Controller`包含一些公共代码,然后是一个`Shopping_Controller`,专门用于购物车的常用功能和数据,以及另一个`Shopping_Specials_Controller`,增加了一些专门的功能......并且所有这些都与`Admin_Controller`(它也从`Base_Controller`继承)分开 - 所以你只能加载你需要的东西。 – 2011-02-17 17:12:25