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 Mssql操作简单封装支持存储过程
Dec 11 PHP
php 操作符与控制结构
Mar 07 PHP
php循环语句 for()与foreach()用法区别介绍
Sep 05 PHP
php从数组中随机抽取一些元素的代码
Nov 05 PHP
解析php框架codeigniter中如何使用框架的session
Jun 24 PHP
php日历制作代码分享
Jan 20 PHP
PHP批量上传图片的具体实现方法介绍.
Feb 26 PHP
Thinkphp调用Image类生成缩略图的方法
Mar 07 PHP
浅谈PHP中如何实现Hook机制
Nov 14 PHP
PHP之多条件混合筛选功能的实现方法
Oct 09 PHP
浅谈Laravel POST,PUT,PATCH 路由的区别
Oct 15 PHP
PHP pthreads v3下worker和pool的使用方法示例
Feb 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
php4的session功能评述(三)
2006/10/09 PHP
php将数据库中的电话号码读取出来并生成图片
2008/08/31 PHP
php中数组首字符过滤功能代码
2012/07/31 PHP
解析关于java,php以及html的所有文件编码与乱码的处理方法汇总
2013/06/24 PHP
通过table标签,PHP输出EXCEL的实现方法
2013/07/24 PHP
PHP编程求最大公约数与最小公倍数的方法示例
2017/05/29 PHP
jquery cookie插件代码类
2009/05/26 Javascript
javascript encodeURI和encodeURIComponent的比较
2010/04/03 Javascript
原始XMLHttpRequest方法详情回顾
2013/11/28 Javascript
javascript实现文本域写入字符时限定字数
2014/02/12 Javascript
jQuery中outerHeight()方法用法实例
2015/01/19 Javascript
JavaScript获取网页支持表单字符集的方法
2015/04/02 Javascript
js判断子窗体是否关闭的方法
2015/08/11 Javascript
Javascript removeChild()删除节点及删除子节点的方法
2015/12/27 Javascript
Javascript字符串拼接小技巧(推荐)
2016/06/02 Javascript
Vue.js动态组件解析
2016/09/09 Javascript
jQuery判断是否存在滚动条的简单方法
2016/09/17 Javascript
微信小程序 location API接口详解及实例代码
2016/10/12 Javascript
微信小程序 限制1M的瘦身技巧与方法详解
2017/01/06 Javascript
Angularjs中数据绑定的实例详解
2017/08/25 Javascript
结合mint-ui移动端下拉加载实践方法总结
2017/11/08 Javascript
element-ui使用导航栏跳转路由的用法详解
2018/08/22 Javascript
深度了解vue.js中hooks的相关知识
2019/06/14 Javascript
如何自定义微信小程序tabbar上边框的颜色
2019/07/09 Javascript
vue中改变滚动条样式的方法
2020/03/03 Javascript
Vue实现附件上传功能
2020/05/28 Javascript
python字典快速保存于读取的方法
2018/03/23 Python
通过Python扫描代码关键字并进行预警的实现方法
2020/05/24 Python
关于Python不换行输出和不换行输出end=““不显示的问题(亲测已解决)
2020/10/27 Python
纽约21世纪百货官网:Century 21
2016/08/27 全球购物
Ramy Brook官网:美国现代女装品牌
2019/06/18 全球购物
2014年社区重阳节活动策划方案
2014/09/16 职场文书
六查六看六改心得体会
2014/10/14 职场文书
消费者理赔投诉书
2015/07/02 职场文书
Django+Nginx+uWSGI 定时任务的实现方法
2022/01/22 Python
《原神》新角色演示“神里绫人:林隐泓洄” 宠妹狂魔
2022/04/03 其他游戏