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 Memcached应用实现代码
Feb 08 PHP
解析左右值无限分类的实现算法
Jun 20 PHP
怎么在Windows系统中搭建php环境
Aug 31 PHP
使用php测试硬盘写入速度示例
Jan 27 PHP
php除数取整示例
Apr 24 PHP
PHP整数取余返回负数的相关解决方法
May 15 PHP
PHP高手需要要掌握的知识点
Aug 21 PHP
PHP实现自动识别原编码并对字符串进行编码转换的方法
Jul 13 PHP
PHP不使用递归的无限级分类简单实例
Nov 05 PHP
详解PHP防止盗链防止迅雷下载的方法
Apr 26 PHP
PHP Laravel 上传图片、文件等类封装
Aug 16 PHP
PHP判断函数是否被定义的方法
Jun 21 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
DOTA2 探索永无止境 玩家自创强悍插眼攻略
2020/04/20 DOTA
咖啡与牛奶
2021/03/03 冲泡冲煮
destoon在360浏览器下出现用户被强行注销的解决方法
2014/06/26 PHP
PHP读MYSQL中文乱码的快速解决方法
2016/10/01 PHP
发现的以前不知道的函数
2006/09/19 Javascript
Javascript常用运算符(Operators)-javascript基础教程
2007/12/14 Javascript
js对文章内容进行分页示例代码
2014/03/05 Javascript
javascript快速排序算法详解
2014/09/17 Javascript
Javascript检查图片大小不要让大图片撑破页面
2014/11/04 Javascript
jQuery插件imgAreaSelect基础讲解
2017/05/26 jQuery
微信小程序实现运动步数排行功能(可删除)
2018/07/05 Javascript
jQuery实现图片简单轮播功能示例
2018/08/13 jQuery
详解基于mpvue微信小程序下载远程图片到本地解决思路
2019/05/16 Javascript
把MySQL表结构映射为Python中的对象的教程
2015/04/07 Python
Python实现分割文件及合并文件的方法
2015/07/10 Python
详解Python设计模式编程中观察者模式与策略模式的运用
2016/03/02 Python
Python中实现最小二乘法思路及实现代码
2018/01/04 Python
Tensorflow 利用tf.contrib.learn建立输入函数的方法
2018/02/08 Python
python+opencv识别图片中的圆形
2020/03/25 Python
python+ffmpeg批量去视频开头的方法
2019/01/09 Python
Python3.5 Json与pickle实现数据序列化与反序列化操作示例
2019/04/29 Python
python3+PyQt5 使用三种不同的简便项窗口部件显示数据的方法
2019/06/17 Python
Python爬虫程序架构和运行流程原理解析
2020/03/09 Python
法国和欧洲海边和滑雪度假:Pierre & Vacances
2017/01/04 全球购物
La Redoute英国官网:法国时尚品牌
2017/04/27 全球购物
阿迪达斯新加坡官方网站:adidas新加坡
2019/12/06 全球购物
高校生生产实习自我鉴定
2013/09/21 职场文书
幼教个人求职信范文
2013/12/02 职场文书
清洁工表扬信
2014/01/08 职场文书
给国外客户的邀请函
2014/01/30 职场文书
西式结婚主持词
2014/03/14 职场文书
群众路线批评与自我批评发言稿
2014/10/16 职场文书
2014年学校团委工作总结
2014/12/20 职场文书
家长通知书家长意见
2014/12/30 职场文书
2015年社区卫生工作总结
2015/04/21 职场文书
vue中data改变后让视图同步更新的方法
2021/03/29 Vue.js