这篇文章主要介绍了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还不太了解吧,等深入一些,再看看有没有解决办法。不过至少目前这个可以工作了。
感谢你能够认真阅读完这篇文章,希望小编分享的“Codeigniter中如何集成smarty和adodb”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。