关于在Codeigniter中集成smarty和adodb的方法解析

不言
发布: 2023-04-01 08:12:01
原创
1607 人浏览过

这篇文章主要介绍了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中文网其他相关文章!

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!