在 application/libraries/ 建立兩個檔案:
Smarty();
$basedir = dirname(__FILE__);
$this->template_dir = "$basedir/templates/";
$this->compile_dir = "$basedir/templates_c/";
$this->config_dir = "$basedir/configs/";
$this->cache_dir = "$basedir/cache/";
$this->debugging = false;
$this->cache_lifetime = 30;
$this->caching = 0;
}
}
?>
adodb =& ADONewConnection($dsn);
$this->adodb->Execute("SET NAMES 'utf8'");
}
}
?>
注意:require 路徑相對於網站根目錄 ($_SERVER['DOCUMENT_ROOT']),而非 libraries 目錄。
在 application/init/ 建立:
mysmarty = new MySmarty(); $obj->ci_is_loaded[] = 'mysmarty'; ?>
adodb = new Adodb($obj); $obj->ci_is_loaded[] = 'adodb'; ?>
class Test extends Controller {
function Test() {
parent::Controller();
$this->load->library('mysmarty');
$this->load->library('adodb');
}
function index() {
$this->load->library('adodb');
$row = $this->adodb->adodb->getrow('SELECT * FROM admin');
$this->mysmarty->assign('row', $row);
$this->mysmarty->display('test.tpl');
}
}
註:$this->adodb->adodb 雙層呼叫因 Library 實例屬性同名,官方建議單層但此處為相容寫法。
更多 CodeIgniter 進階教學請參考本站專題:《codeigniter入門教程》、《CI(CodeIgniter)框架進階教程》、《php日期與時間用法總結》、《php面向對象程序設計入門教程》、《php字符串(string)用法總結》、《php+mysql數據庫操作入門教程》及《php常見數據庫操作技巧匯總》。