上一篇日志谈到为Codeigniter 配置第三方类库,写了几个文件以后觉得每次写类的构造函数时需要
- $this->load->library("class");
感觉这样很繁琐,就想到应该有配置能够给Codeigniter 自动加载一些类库
google一下。从官网的manual得知在config/autoload.php可配置自动加载的类库、模型等
- /*
- | -------------------------------------------------------------------
- | Auto-load Libraries
- | -------------------------------------------------------------------
- | These are the classes located in the system/libraries folder
- | or in your system/application/libraries folder.
- |
- | Prototype:
- |
- | $autoload['libraries'] = array('database', 'session', 'xmlrpc');
- */
- $autoload['libraries'] = array();
把需要自动加载的类库填到数组里就行了,不需要指定索引
- $autoload['libraries'] = array("database","mivec"); //initiallizationed MIVEC's class in here
在打开控制器自己写的类。。就可以把以下代码去掉
- $this->load->database();
- $this->load->library("mivec");
- class Index extends Controller {
- public function __construct(){
- parent::Controller();
- $this->mivec->initdb($this->db);
- }
- public function index(){
- $obj = new Stdclass;
- $this->db->query("SELECT COUNT(*) FROM x_video");
- $row = $this->mivec->db->getRowsData("x_video","id,title","","",20,1);
- if (is_array($row)) {
- foreach ($row as $rs) {
- $obj->arr[] = (object) array(
- "id" => $rs["id"],
- "title" => $rs["title"]
- );
- }
- $this->mivec->smarty->assign("videos",$obj->arr);
- $this->mivec->smarty->assign("page",$this->mivec->smarty->getRowsDataPager());
- $this->mivec->smarty->display("video/index.html");
- }
- }
- }

本文介绍了如何在Codeigniter框架中简化第三方类库的加载过程。通过配置config/autoload.php文件,可以实现自动加载类库、模型等功能,避免在每个类的构造函数中手动加载。

2507

被折叠的 条评论
为什么被折叠?



