Codeigniter中集成smarty和adodb的方法


Posted in PHP onMarch 04, 2016

本文实例讲述了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('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 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 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 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
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,按照官方的做法应该只需要一次,但是他的方法在我这里有错误。可能是我对CodeIgniter还不太了解吧,等深入一些,再看看有没有解决办法。不过至少目前这个可以工作了。

希望本文所述对大家PHP程序设计有所帮助。

PHP 相关文章推荐
把PHP安装为Apache DSO
Oct 09 PHP
Ajax PHP简单入门教程代码
Apr 25 PHP
一个PHP验证码类代码分享(已封装成类)
Jul 17 PHP
关于php连接mssql:pdo odbc sql server
Jul 20 PHP
PHP5 的对象赋值机制介绍
Aug 02 PHP
深入了解 register_globals (附register_globals=off 网站打不开的解决方法)
Jun 27 PHP
php中拷贝构造函数、赋值运算符重载
Jul 25 PHP
php使用pack处理二进制文件的方法
Jul 03 PHP
PHP的时间戳与具体时间转化的简单实现
Jun 13 PHP
php实现的读取CSV文件函数示例
Feb 07 PHP
thinkphp自定义权限管理之名称判断方法
Apr 01 PHP
php插件Xajax使用方法详解
Aug 31 PHP
PHP常用技巧汇总
Mar 04 #PHP
将PHP程序中返回的JSON格式数据用gzip压缩输出的方法
Mar 03 #PHP
PHP的数组中提高元素查找与元素去重的效率的技巧解析
Mar 03 #PHP
CodeIgniter针对数据库的连接、配置及使用方法
Mar 03 #PHP
CodeIgniter表单验证方法实例详解
Mar 03 #PHP
PHP6新特性分析
Mar 03 #PHP
php轻松实现文件上传功能
Mar 03 #PHP
You might like
最小化数据传输――在客户端存储数据
2006/10/09 PHP
php文件管理基本功能简单操作
2017/01/16 PHP
PHP让网站移动访问更加友好方法
2019/02/14 PHP
用于自动添加Digg This!按钮的JavaScript
2006/12/23 Javascript
如何简单地用YUI做JavaScript动画
2007/03/10 Javascript
JSON 学习之完全手册 图文
2007/05/29 Javascript
js注意img图片的onerror事件的分析
2011/01/01 Javascript
利用try-catch判断变量是已声明未声明还是未赋值
2014/03/12 Javascript
javascript实例分享---具有立体效果的图片特效
2014/06/08 Javascript
谷歌Chrome浏览器扩展程序开发小记
2016/01/06 Javascript
基于jquery实现简单的分页控件
2016/03/17 Javascript
javascript self对象使用详解
2016/10/18 Javascript
通过sails和阿里大于实现短信验证
2017/01/04 Javascript
requireJS模块化实现返回顶部功能的方法详解
2017/10/16 Javascript
详谈构造函数加括号与不加括号的区别
2017/10/26 Javascript
在vue-cli搭建的项目中增加后台mock接口的方法
2018/04/26 Javascript
原生JS实现旋转轮播图+文字内容切换效果【附源码】
2018/09/29 Javascript
angular多语言配置详解
2019/05/16 Javascript
关于微信小程序获取小程序码并接受buffer流保存为图片的方法
2019/06/07 Javascript
原生js实现照片墙效果
2020/10/13 Javascript
[01:48]帕吉至宝加入游戏,遗迹战场现“千劫神屠”
2018/04/07 DOTA
[46:23]OG vs EG 2018国际邀请赛淘汰赛BO3 第一场 8.23
2018/08/24 DOTA
python二分法实现实例
2013/11/21 Python
Python实现Const详解
2015/01/27 Python
对python中return和print的一些理解
2017/08/18 Python
Python使用ctypes调用C/C++的方法
2019/01/29 Python
解决Pytorch自定义层出现多Variable共享内存错误问题
2020/06/28 Python
python中time.ctime()实例用法
2021/02/03 Python
CSS3制作苹果风格键盘特效
2015/02/26 HTML / CSS
EJB包括(SessionBean,EntityBean)说出他们的生命周期,及如何管理事务的
2015/07/24 面试题
肯尼迪就职演说稿
2013/12/31 职场文书
房产委托公证书
2014/04/08 职场文书
银行进社区活动总结
2014/07/07 职场文书
2015年度班主任自我评价
2015/03/11 职场文书
学生退学证明
2015/06/23 职场文书
CSS3中Animation实现简单的手指点击动画的示例
2021/07/15 HTML / CSS