Codeigniter 自动加载第三方类

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

上一篇日志谈到为Codeigniter 配置第三方类库,写了几个文件以后觉得每次写类的构造函数时需要

  1. $this->load->library("class");

感觉这样很繁琐,就想到应该有配置能够给Codeigniter 自动加载一些类库

google一下。从官网的manual得知在config/autoload.php可配置自动加载的类库、模型等

  1. /*
  2. | -------------------------------------------------------------------
  3. |  Auto-load Libraries
  4. | -------------------------------------------------------------------
  5. | These are the classes located in the system/libraries folder
  6. | or in your system/application/libraries folder.
  7. |
  8. | Prototype:
  9. |
  10. |   $autoload['libraries'] = array('database', 'session', 'xmlrpc');
  11. */
  12. $autoload['libraries'] = array();

把需要自动加载的类库填到数组里就行了,不需要指定索引

 

 

  1. $autoload['libraries'] = array("database","mivec"); //initiallizationed MIVEC's class in here

在打开控制器自己写的类。。就可以把以下代码去掉

  1. $this->load->database();
  2. $this->load->library("mivec");
  1. class Index extends Controller {
  2.     
  3.     public function __construct(){
  4.         parent::Controller();
  5.        $this->mivec->initdb($this->db); 
  6.     }
  7.     
  8.     public function index(){
  9.         $obj = new Stdclass;
  10.         $this->db->query("SELECT COUNT(*) FROM x_video");
  11.         $row = $this->mivec->db->getRowsData("x_video","id,title","","",20,1);
  12.         
  13.         if (is_array($row)) {
  14.             foreach ($row as $rs) {
  15.                 $obj->arr[] = (object) array(
  16.                     "id" => $rs["id"],
  17.                     "title" => $rs["title"]
  18.                 );
  19.             }
  20.             
  21.             $this->mivec->smarty->assign("videos",$obj->arr);
  22.             $this->mivec->smarty->assign("page",$this->mivec->smarty->getRowsDataPager());
  23.             
  24.             $this->mivec->smarty->display("video/index.html");
  25.         }
  26.     }
  27. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值