【发布时间】:2013-02-06 15:25:47
【问题描述】:
在 CodeIgniter 中,我经常有许多项目固有的脚本,例如:
<?php
// Load many things
$this->load->model('news_model');
$this->load->helper('utility_helper');
$news = $this->news_model->get_basic_news();
// For moment no news
$view_datas['news']['check'] = false;
if ($news) {
$view_datas['news'] = array(
'check' => true,
'news' => _humanize_news($news)
);
}
?>
这个脚本在不同的控制器中使用,目前我创建了一个scripts 文件夹并像这样导入它:include(APPPATH . 'scripts/last_news.php'); 我很确定这不是处理这个问题的最佳方法。对此有什么想法吗?
更新:
答案中给出的解决方案是使用helper 或library。
让我们想象一下我之前的代码的重写:
class Scripts {
public function last_news() {
// Load many things to use
$CI =& get_instance();
$CI->load->model('news_model');
$CI->load->model('utility_helper');
$news = $CI->news_model->get_basic_news();
// Avoid the rest of code
}
}
【问题讨论】:
-
我通常制作这些助手......并将它们包含在自动加载中
标签: php codeigniter scripting