目前CI框架已经推出了2.0的版本了。一直想尝试修改我的网站的核心。经过测试可以正常使用,以下提供配置方案和已经配置好的文件,
下面是配置步骤的详细说明一下:
第一步:安装CodeIngiter。 这个不需要详细说,下载地址为:http://codeigniter.com/downloads/
第二步:下载最新版本的 Smarty库,下载地址:http://www.smarty.net/download
第三步:在安装好的ci项目的根目录下的入口文件 index.php文件开头添加如下代码
define('APP_NAME','application');
if(!defined('ROOT'))define('ROOT',dirname(__FILE__));
if (!defined('DS')) define('DS', DIRECTORY_SEPARATOR);
第四步:在安装好的ci项目的根目录下application\config下添加smarty.php配置文件
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/* smarty 设置 */
$config['caching']= false;
$config['cache_lifetime']= 3600;
$config['template_dir'] = ROOT.DS.APP_NAME.DS.'views'.DS;
$config['compile_dir'] = ROOT.DS.APP_NAME.DS.'cache'.DS.'views_c'.DS;
$config['cache_dir '] = ROOT.DS.APP_NAME.DS.'cache'.DS.'smarty_cache'.DS;
$config['left_delimiter'] = '';
第五步:在安装好的ci项目的根目录下\application\libraries,把下载好的smarty文件包中 libs 放到这里,并改名为smarty
第六步:在安装好的ci项目的根目录下\application\libraries下添加smarty扩展类CI_Smarty.php
require (ROOT.DS.APPPATH."libraries".DS."smarty".DS."Smarty.class.php");
class CI_Smarty extends Smarty
{
protected $ci;
public function __construct()
{
parent::__construct();
$this->ci = & get_instance();
$this->ci->load->config('smarty');
self::loadSetting();
}
public function loadSetting()
{
$this->caching= $this->ci->config->item('caching');
$this->cache_lifetime= $this->ci->config->item('cache_lifetime');
$this->template_dir= $this->ci->config->item('template_dir');
$this->compile_dir = $this->ci->config->item('compile_dir');
$this->cache_dir = $this->ci->config->item('cache_dir');
$this->left_delimiter = $this->ci->config->item('left_delimiter');
$this->right_delimiter = $this->ci->config->item('right_delimiter');
}
}
第七步:在application\config\autoload.php,设置自动加载smarty扩展类CI_Smarty.php
$autoload['libraries'] = array('ci_smarty');
第八步:在application\core 添加 MY_Controller.php 文件
class MY_Controller extends CI_Controller
{
public function __construct()
{
parent::__construct();
}
public function assign($key,$val)
{
$this->ci_smarty->assign($key,$val);
}
public function display($html)
{
$this->ci_smarty->display($html);
}
}
第九步:已经配置完成 开始测试 在 application\controllers\welcome.php
class Welcome extends MY_Controller {
public function index()
{
$this->assign("hello","我的博客");
$this->display("test.html");
}
}
模板文件 \application\views\test.html
访问你的本地 index.php 就可以看到