用户
 找回密码
 入住 CI 中国社区
搜索
查看: 7345|回复: 4
收起左侧

转发一篇文章:在CodeIgniter 中集成smarty和adodb

[复制链接]
发表于 2008-3-26 13:35:29 | 显示全部楼层 |阅读模式
这本应该是昨晚的日志,结果CDMA不争气,连续拨号到凌晨2点,也无济于事,只好今天来写了。

在众多的PHP Framework中,我暂时考虑选择code igniter,因为我喜欢它的简洁,而且在我目前的项目中,用CI(code igniter的简称,下同)就足够了。

以前我的php程序都使用了smarty和adodb,而CI并不支持他们,CI有自己的数据库操作类(模式里)和模版类(视图里),但是我的项目已经用了adodb和smarty了,因此我希望能集成进来。

去官方论坛搜索了一下,还是有这方面的介绍。照着上面的指导,然后根据自己的理解,目前在CI上已经能够使用smarty和adodb。

因为我也是刚开始使用CI,所以不太理解它内部的机制,因此这里就只写出集成的过程(其他的我也不知道了)。

在CI中要集成第三方的工具,就需要写自己的库。官方手册有详细的介绍。

要写自己的库,就需要写两个文件,一个是在application/init下面的init_myclass.php文件(如果没有init目录,自己创建)。另外一个就是在application/libraries目录下创建myclass.php文件。
这里myclass是你的类名。一些规则大家看手册就好了,我这里直接就说步骤了。
1)在application/libraries下分别创建mysmarty.php和adodb.php
mysmarty.php文件的内容如下:
PHP复制代码
 
<?php
 
// load Smarty library
 
require('Smarty/Smarty.class.php');
 
// The setup.php file is a good place to load
// required application library files, and you
// can do that right here. An example:
// require('guestbook/guestbook.lib.php');
 
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('app_name', 'Guest Book');
   }
 
}
?>
 
 
复制代码


文件路径根据具体情况修改,文件的的路径是相对你的网站的主目录开始的,而不是当前文件的当前目录,比如上面的require('Smarty/Smarty.class.php');不是相对application/libraries目录,而是相对$_SERVER['DOCUMENT_ROOT']目录。
adodb.php文件的内容如下:

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

2)在application/init目录下分别创建init_adodb.php和init_mysmarty.php。
init_adodb.php文件内容如下:
PHP复制代码
 
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
 
$obj =& get_instance();
$obj->adodb = new Adodb($obj);
$obj->ci_is_loaded[] = 'adodb';
 
 
复制代码


init_mysmarty.php文件内容如下:
PHP复制代码
 
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
 
if ( ! class_exists('MySmarty'))
{
     require_once(APPPATH.'libraries/mysmarty'.EXT);
}
 
$obj =& get_instance();
$obj->mysmarty = new MySmarty();
$obj->ci_is_loaded[] = 'mysmarty';
 
?>
 
 
复制代码


3)使用他们
在application/controllers目录下创建一个你需要的文件,你可以这样来使用adodb和smarty。
PHP复制代码
 
 
<?php
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");
  }
 
}
?>
 
 
复制代码


我也不知道这里为什么需要两次adodb,按照官方的做法应该只需要一次,但是他的方法在我这里有错误。可能是我对CI还不太了解吧,等深入一些,再看看有没有解决办法。不过至少目前这个可以工作了。



我也不知道这里为什么需要两次adodb
把 $this->adodb =& ADONewConnection($dsn);
改成 $this =& ADONewConnection($dsn);
只需要一次adodb

评分

参与人数 1威望 +1 收起 理由
Hex + 1 精品文章

查看全部评分

 楼主| 发表于 2008-3-26 17:29:19 | 显示全部楼层
谢谢,Hex,
大家一起把这个做好哈
发表于 2008-3-30 17:37:39 | 显示全部楼层
集成SMARTY可以看FACEBOOK_CLONE的源码
方法与LZ转的差不多
发表于 2010-9-10 22:29:12 | 显示全部楼层
我正在学习中。。。
发表于 2010-9-11 21:28:57 | 显示全部楼层
想知道下,test.tpl文件是放在templates下,还是放在view下呢?

本版积分规则