首頁  >  文章  >  後端開發  >  關於在Codeigniter中整合smarty和adodb的方法解析

關於在Codeigniter中整合smarty和adodb的方法解析

不言
不言原創
2018-06-14 15:18:141586瀏覽

這篇文章主要介紹了Codeigniter中集成smarty和adodb的方法,結合實例形式分析了Codeigniter庫的使用技巧,需要的朋友可以參考下

本文實例講述了Codeigniter中集成smarty和adodb的方法。分享給大家供大家參考,具體如下:

在CodeIgniter中要寫自己的庫,就需要寫兩個文件,一個是在application/init下面的init_myclass.php文件(如果沒有init目錄,自己創建)。另外一個就是在application/libraries目錄下建立myclass.php檔案。

這裡myclass是你的類別名稱。有些規則大家看手冊就好了,我這裡直接就說步驟了。

1)在application/libraries下分別建立mysmarty.php和adodb.php
mysmarty.php檔案的內容如下:

<?php
// load Smarty library
require(&#39;Smarty/Smarty.class.php&#39;);
// The setup.php file is a good place to load
// required application library files, and you
// can do that right here. An example:
// require(&#39;guestbook/guestbook.lib.php&#39;);
class MySmarty extends Smarty {
 function MySmarty()
 {
    // Class Constructor.
    // These automatically get set with each new instance.
    $this->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->compile_check = true;
    //this is handy for development and debugging;never be used in a production environment.
    //$smarty->force_compile=true;
    $this->debugging = false;
    $this->cache_lifetime=30;
    $this->caching = 0; // lifetime is per cache
    //$this->assign(&#39;app_name&#39;, &#39;Guest Book&#39;);
 }
}
?>

檔案路徑根據具體情況修改,檔案的的路徑是相對你的網站的主目錄開始的,而不是當前檔案的當前目錄,例如上面的require('Smarty/Smarty.class.php');不是相對application/libraries目錄,而是相對$_SERVER['DOCUMENT_ROOT']目錄。

adodb.php檔案的內容如下:

<?php if (!defined(&#39;BASEPATH&#39;)) exit(&#39;No direct script access allowed&#39;);
class Adodb
{
  function Adodb()
  {
    //$dsn="dbdriver://username:password@server/database"
    $dsn = &#39;mysql://user:password@localhost/xxxx&#39;;
    require_once("adodb/adodb.inc".EXT);
    $this->adodb =& ADONewConnection($dsn);
    $this->adodb->Execute("set NAMES &#39;utf8&#39;"); 
  }
}
?>

2)在application/init目錄下分別建立init_adodb.php和init_mysmarty.php 。

init_adodb.php檔案內容如下:

<?php if (!defined(&#39;BASEPATH&#39;)) exit(&#39;No direct script access allowed&#39;);
$obj =& get_instance();
$obj->adodb = new Adodb($obj);
$obj->ci_is_loaded[] = &#39;adodb&#39;;

init_mysmarty.php檔案內容如下:

#
<?php if (!defined(&#39;BASEPATH&#39;)) exit(&#39;No direct script access allowed&#39;);
if ( ! class_exists(&#39;MySmarty&#39;))
{
  require_once(APPPATH.&#39;libraries/mysmarty&#39;.EXT);
}
$obj =& get_instance();
$obj->mysmarty = new MySmarty();
$obj->ci_is_loaded[] = &#39;mysmarty&#39;;
?>

3)使用他們
在application/controllers目錄下建立一個你需要的文件,你可以這樣來使用adodb和smarty。

<?php
class Test extends Controller {
 function Test()
 {
  parent::Controller(); 
  $this->load->library(&#39;mysmarty&#39;);
  $this->load->library(&#39;adodb&#39;);
 }
 function index()
 {
 $this->load->library(&#39;adodb&#39;);
 $row = $this->adodb->adodb->getrow(&#39;SELECT * FROM admin&#39;);
    $this->mysmarty->assign("row",$row);
    $this->mysmarty->display("test.tpl");
 }
}
?>

我也不知道這裡為什麼需要兩次adodb,按照官方的做法應該只需要一次,但是他的方法在我這裡有錯誤。可能是我對CodeIgniter還不太了解吧,等深入一些,再看看有沒有解決方法。不過至少目前這個可以工作了。

以上就是本文的全部內容,希望對大家的學習有所幫助,更多相關內容請關注PHP中文網!

相關推薦:

關於PHP的Symfony和CodeIgniter框架的Nginx重寫規則配置

##關於CI框架中$this->load->library()的用法分析

##############################

以上是關於在Codeigniter中整合smarty和adodb的方法解析的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn