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 mysql索引问题
Jun 07 PHP
DedeCMS dede_channeltype表字段注释
Apr 07 PHP
PHP跨时区(UTC时间)应用解决方案
Jan 11 PHP
基于PHP常用函数的用法详解
May 10 PHP
Zend Framework页面缓存实例
Jun 25 PHP
php防止伪造数据从地址栏URL提交的方法
Aug 24 PHP
php实现屏蔽掉黑帽SEO的搜索关键字
Apr 15 PHP
php similar_text()函数的定义和用法
May 12 PHP
CI框架支持$_GET的两种实现方法
May 18 PHP
PHP中spl_autoload_register()函数用法实例详解
Jul 18 PHP
php面向对象程序设计中self与static的区别分析
May 21 PHP
PHP实现基于状态的责任链审批模式详解
May 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
escape unescape的php下的实现方法
2007/04/27 PHP
php chr() ord()中文截取乱码问题解决方法
2008/09/08 PHP
UTF-8正则表达式如何匹配汉字
2015/08/03 PHP
浅析ThinkPHP缓存之快速缓存(F方法)和动态缓存(S方法)(日常整理)
2015/10/26 PHP
WordPress中用于获取搜索表单的PHP函数使用解析
2016/01/05 PHP
PHP入门教程之正则表达式基本用法实例详解(正则匹配,搜索,分割等)
2016/09/11 PHP
Javascript 篱式条件判断
2008/08/22 Javascript
js中函数调用的两种常用方法使用介绍
2014/07/17 Javascript
js点击选择文本的方法
2015/02/09 Javascript
javascript实现网页背景烟花效果的方法
2015/08/06 Javascript
json+jQuery实现的无限级树形菜单效果代码
2015/08/27 Javascript
JavaScript使用DeviceOne开发实战(四)仿优酷视频应用
2015/12/02 Javascript
KnockoutJS 3.X API 第四章之数据控制流if绑定和ifnot绑定
2016/10/10 Javascript
将angular.js项目整合到.net mvc中的方法详解
2017/06/29 Javascript
微信小程序中使用Promise进行异步流程处理的实例详解
2017/08/17 Javascript
Vue Transition实现类原生组件跳转过渡动画的示例
2017/08/19 Javascript
React 组件转 Vue 组件的命令写法
2018/02/28 Javascript
js循环map 获取所有的key和value的实现代码(json)
2018/05/09 Javascript
React Native基础入门之调试React Native应用的一小步
2018/07/02 Javascript
微信小程序开发之转发分享功能
2019/10/22 Javascript
vue改变循环遍历后的数据实例
2019/11/07 Javascript
Vue项目中使用jsonp抓取跨域数据的方法
2019/11/10 Javascript
[53:10]2018DOTA2亚洲邀请赛 4.6 淘汰赛 VP vs VG 第一场
2018/04/11 DOTA
利用python获取Ping结果示例代码
2017/07/06 Python
Tensorflow 同时载入多个模型的实例讲解
2018/07/27 Python
Python中单线程、多线程和多进程的效率对比实验实例
2019/05/14 Python
pycharm 复制代码出现空格的解决方式
2021/01/15 Python
详解canvas drawImage()方法绘制图片不显示的问题
2018/10/08 HTML / CSS
马德里著名的运动鞋商店:NOIRFONCE
2019/04/12 全球购物
Nike澳大利亚官网:Nike.com (AU)
2019/06/03 全球购物
Bonami斯洛伐克:购买家具和家居饰品
2019/07/02 全球购物
北京某科技有限公司C# .net笔试题
2014/09/27 面试题
党员学习新党章思想汇报
2014/10/25 职场文书
户外活动总结
2015/02/04 职场文书
《我的伯父鲁迅先生》教学反思
2016/02/16 职场文书
python机器学习Github已达8.9Kstars模型解释器LIME
2021/11/23 Python