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 相关文章推荐
初学者入门:细述PHP4的核心Zend
Sep 05 PHP
动态网站web开发 PHP、ASP还是ASP.NET
Oct 09 PHP
php入门教程 精简版
Dec 13 PHP
PHP与MySQL开发的8个技巧小结
Dec 17 PHP
深入探讨PHP中的内存管理问题
Aug 31 PHP
PHP中对缓冲区的控制实现代码
Sep 29 PHP
php中的boolean(布尔)类型详解
Oct 28 PHP
CI框架自动加载session出现报错的解决办法
Jun 17 PHP
destoon安全设置中需要设置可写权限的目录及文件
Jun 21 PHP
PHP实现无限极分类图文教程
Nov 25 PHP
php判断文件上传类型及过滤不安全数据的方法
Dec 17 PHP
PHP中__set()实例用法和基础讲解
Jul 23 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
PHP MVC模式在网站架构中的实现分析
2010/03/04 PHP
thinkPHP引入类的方法详解
2016/12/08 PHP
laravel 5.4 + vue + vux + element的环境搭配过程介绍
2018/04/26 PHP
php使用socket调用http和smtp协议实例小结
2019/07/26 PHP
thinkphp3.2框架中where条件查询用法总结
2019/08/13 PHP
记录几个javascript有关的小细节
2007/04/02 Javascript
eclipse导入jquery包后报错的解决方法
2014/02/17 Javascript
使用JavaScript实现旋转的彩圈特效
2015/06/23 Javascript
JS实现可展开折叠层的鼠标拖曳效果
2015/10/09 Javascript
AngularJS使用带属性值的ng-app指令实现自定义模块自动加载的方法
2017/01/04 Javascript
JavaWeb表单及时验证功能在输入后立即验证(含用户类型,性别,爱好...的验证)
2017/06/09 Javascript
自定义事件解决重复请求BUG的问题
2017/07/11 Javascript
简单实现js放大镜效果
2017/07/24 Javascript
移动端触摸滑动插件swiper使用方法详解
2017/08/11 Javascript
ECMAscript 变量作用域总结概括
2017/08/18 Javascript
vue基于mint-ui的城市选择3级联动的示例
2017/10/25 Javascript
Javascript中的作用域及块级作用域
2017/12/08 Javascript
vue组件name的作用小结
2018/05/23 Javascript
AngularJS中ng-options实现下拉列表的数据绑定方法
2018/08/13 Javascript
详解关于element el-button使用$attrs的一个注意要点
2018/11/09 Javascript
vue中的计算属性和侦听属性
2020/11/06 Javascript
python 布尔操作实现代码
2013/03/23 Python
Python读写Excel文件的实例
2013/11/01 Python
PyCharm汉化安装及永久激活详细教程(靠谱)
2020/01/16 Python
python 串行执行和并行执行实例
2020/04/30 Python
德国高品质男装及配饰商城:Cultizm(Raw Denim原色牛仔裤)
2018/04/16 全球购物
nohup的用法
2012/11/26 面试题
电脑教师的教学自我评价
2013/11/26 职场文书
机修工工作职责
2014/02/21 职场文书
环境工程专业毕业生求职信
2014/09/30 职场文书
股东大会通知
2015/04/24 职场文书
图书借阅制度范本
2015/08/06 职场文书
《称赞》教学反思
2016/02/17 职场文书
2016年第29个世界无烟日宣传活动总结
2016/04/06 职场文书
前端监听websocket消息并实时弹出(实例代码)
2021/11/27 Javascript
Windows Server 2022 超融合部署(图文教程)
2022/06/25 Servers