CI和Smarty整合并且前后台加载不同配置文件使前台应用Smarty缓存后台不应用
来源:互联网 发布:淘宝网一元拍卖 编辑:程序博客网 时间:2024/05/24 05:37
即将大四毕业的准PHPer,技术有限,能力有限,但是一心热爱编程,大学四年的时间基本全部都花费在学习PHP这上面了,可惜人没那么聪明,学了四年,对于Linux还是没有熟悉,对于服务器还是不能掌握的很好,对于数据库还只站在门外看看,对于PHP语言还没有达到精通的地步。。。实在不太好意思在人前露脸!
可是我一想到我就要毕业了,四年不是宅在宿舍开码就是在课堂上睡觉,差点儿就变成了纯屌丝,当别人各种参加招聘会的时候,我选择了找个消费低,工资低,但是省着点儿用一个月可以攒2k多的地方提前出来工作了,只因为自己一心钟爱Beyond,喜欢黄家驹,想能够凭借自己的一点点努力在毕业前赚够路费去一趟香港也算是大学无憾。在一家不主要做Web开发的公司,来的时候技术员因为嫌弃地方小走了,也确实如此神人怎么会委身在这个地方,可怜我一个初出茅庐的小毛头要接下大神的工作继续往下做,首先是必须熟悉linux,用来维护服务器,然后就是必须熟悉CI和Smarty,Smarty还好,但是CI我还是第一次听说,然后就是能够做微信公众平台的开发。当熟悉这些之后就是要维护大神做好的四个站点能够再原基础上再开发,大神不愧是大神,四个站点一个后台搞定,权限分配也是很有讲究,没办法一切都得上,一个星期的时间天天提早上班1个小时晚上在公司做到9点多,还得走一个多小时回家之后还得接着弄。。。为什么是一个星期,因为我给自己定了时间,因为我一直觉得小公司不培养人,好点儿的培养也最多一个星期就能够给人做东西,不行那接下来就随时有被抛弃的感觉,不好的上来面试就问能不能做,不能做就算了,试用的机会都没有,要么就是工资低的死。。。。
综上所述,我决定把这篇文章推荐到首页,因为我觉得如果我不主动点放上去这东西就只能留着自己看了。。。
其实另外一个原因就是早上看到“沈逸”的文字,说是写篇文字吸引十个评论,好吧,我尝试下看看,反正毕业了。。。什么丢脸的事儿都能尝就试试看看,反正也不违法。。
CI框架和Smarty的整合,利用了CI框架的轻小而且性能高,简单易学,再加上Smarty的缓存功能,这种搭配的本身性能已经很高了,如果服务器给点力的话一般就不需要多做什么了,如果还有更高的要求再加上一种NoSql像memcache或者redis,不是很大的并发一般没什么问题。
好吧,作为一个菜鸟,我也大概能描述这么多,纯粹的自我感觉,大神莫喷。。。回归正题吧!
做CI和Smarty的整合比较简单,利用的就是CI的类库加载方法library,然后给smarty做一些配置就整合成功了,直接贴代码吧!
require 'libs/Smarty.class.php';class Frontsmarty extends Smarty {/** * @todo构造方法,配置Smarty */public function Frontsmarty() {parent::Smarty();$config =& get_config();$this->plugins_dir = $config['smarty_plugins_dir'];//smarty插件目录,用于做局部缓存$this->compile_check = $config['smarty_compile_check'];$this->left_delimiter = $config['smarty_left_delimiter'];$this->right_delimiter = $config['smarty_right_delimiter'];$this->caching = $config['smarty_caching'];$this->cache_lifetime = $config['smarty_cache_lifetime'];$this->template_dir = $config['smarty_template_dir'];$this->cache_dir = $config['smarty_cache_dir'];$this->compile_dir = $config['smarty_compile_dir'];}/** * @todo调用模板展示 * @param string $resource_name * @param string $cache_id */public function view($resource_name, $cache_id = null) {parent::display($resource_name, $cache_id);}}
在目录application/libraries下新建一个Frontsmarty.php文件将上述代码放进去就可以了。
注意:文件名首字母必须是大写
说明:这里我文件名叫Frontsmarty.php是因为我在后面使用smarty缓存的时候需要把前后台分开,因为后台是不需要缓存的
经过上面的配置,如果你不想做后台的话那么CI和Smarty的整合就完成了,只需要在控制器的构造方法中通过$this->load->library('frontsmarty')加载一下这个类就可以使用了。
那我这里主要是想使用Smarty的缓存,并且使得后台不具有缓存功能,所以我还得继续往下做,
那接下来也比较简单,就是在同一个目录下新建另外一个文件名为:Adminsmarty.php,然后在里面贴上如下的代码:
require 'libs/Smarty.class.php';class Adminsmarty extends Smarty {/** * @todo构造方法,配置Smarty */public function Adminsmarty() {parent::Smarty();//关于CI的自动装载资源,例如自动装载配置文件,则在指定的配置文件中的数值依然是$config$config =& get_admin_config();$this->compile_check = $config['smarty_compile_check'];$this->left_delimiter = $config['smarty_left_delimiter'];$this->right_delimiter = $config['smarty_right_delimiter'];$this->template_dir = $config['smarty_template_dir'];$this->compile_dir = $config['smarty_compile_dir'];}/** * @todo调用模板展示 * @param string $resource_name * @param string $cache_id */public function view($resource_name, $cache_id = null) {parent::display($resource_name, $cache_id);}}
注意上面标红的那个函数,它就是用来调用后台的配置文件,注意这个方法是直接调用的,说明他已经提前加载了,那是什么时候加载呢?这就是下面的工作了。
那在继续之前你得先明白CI框架的核心类时允许程序员扩展的或者说它的核心代码程序员是可以通过自定义的类直接覆盖的,从而从本质上改变CI的核心。那通过什么样的方法可以实现呢,这个在CI的手册中有提供,就是在application/core目录下新建一个以MY_Xxx开头的类文件,这里的MY_是可以通过配置文件自定义的,Xxx则是你想重写的CI核心类文件,例如我这里需要重写的就是Controller那么Xxx==Controller。
当弄明白上面的东西之后就继续我们的工作,明白了也就很简单了,在application/core下新建文件名为:MY_Controller.php,然后贴上下面的代码:
class Admin_Controller extends CI_Controller {public function __construct() {parent::__construct();$this->load->helper('admin');//加载后台辅助函数,用于加载自定义后台配置文件$this->load->library('adminsmarty');}}class Front_Controller extends CI_Controller {public function __construct() {parent::__construct();$this->load->library('frontsmarty');}}
还是注意上面标红的代码,上面提出的问题是我们的get_admin_config()方法是什么时候加载的,那这段代码就告诉了你加载的时机和方法,就是通过CI的加载辅助方法,然后在调用后台控制器的时候来加载,当然这里必须得有一个单利,下面的代码就是get_admin_config()方法:
/** * 加载后台配置文件 */if ( ! function_exists('get_admin_config') ) {function &get_admin_config() {static $_config;if ( isset($_config) ) {return $_config[0];}require(APPPATH . 'config/admin/adminconfig.php');return $_config[0] =& $admin_config;}}
标红的代码标示后台配置文件的路径,这个你可以自定义。这个方法放置的位置是:application/helpers/admin_helper.php(文件名必须这么写,因为你通过$this->load->helper()方法加载的时候是指定的“admin”,当然你指定别的名字那文件名就又要变了,但是_helper是必须的)
class Welcome extends Front_Controller {public function __construct() {parent::__construct();}public function index(){$_tpl_name = 'home/test.html';if ( !$this->frontsmarty->is_cached($_tpl_name) ) {$this->frontsmarty->assign('koma', 'i love beyond');}$arr = array('test1' => 'name1','test2' => 'name2','test3' => 'name3','test4' => 'name4','test5' => 'name4','test6' => 'name4','test7' => 'name4','test8' => 'name4','test9' => 'name9','test10' => 'name9','test11' => 'nam11');$this->frontsmarty->assign('myarr', $arr);$this->frontsmarty->assign('mydata', date('Y-m-d H:i:s', time()));$this->frontsmarty->view($_tpl_name);}}
这里要注意的就是控制器继承的类,不是MY_Controller也不是CI_Controller。下面是后台控制器调用示例代码:
class Admin extends Admin_Controller {public function __construct() {parent::__construct();}public function index(){$_tpl_name = 'admin/index.html';if ( !$this->adminsmarty->is_cached($_tpl_name) ) {$this->adminsmarty->assign('koma', 'i love beyond');}$arr = array('test1' => 'name1','test2' => 'name2','test3' => 'name3','test4' => 'name4','test5' => 'name4','test6' => 'name4','test7' => 'name4','test8' => 'name4','test9' => 'name9','test10' => 'name9','test11' => 'nam11');$this->adminsmarty->assign('myarr', $arr);$this->adminsmarty->view($_tpl_name);}}
下面我贴出前台和后台smarty配置文件的代码:
$config['smarty_compile_check'] = false;//是否强制编译模板文件:在产品上线之后建议关闭$config['smarty_left_delimiter'] = '<{';$config['smarty_right_delimiter'] = '}>';$config['smarty_caching'] = true;//当设置缓存生效时间时这个值只要不为0就可以$config['smarty_cache_lifetime'] = 10;//默认缓存时间:3600秒$config['smarty_template_dir'] = APPPATH . 'views/';$config['smarty_cache_dir'] = APPPATH . 'cache/';$config['smarty_compile_dir'] = APPPATH . 'complie/';$config['smarty_plugins_dir'] = APPPATH . 'helpers/';
后台smarty的配置文件:
$admin_config['smarty_compile_check'] = false;//是否强制编译模板文件:在产品上线之后建议关闭$admin_config['smarty_left_delimiter'] = '<{';$admin_config['smarty_right_delimiter'] = '}>';$admin_config['smarty_template_dir'] = APPPATH . 'views/';$admin_config['smarty_compile_dir'] = APPPATH . 'complie/';
至此,整个整合以及配置完成。
- CI和Smarty整合并且前后台加载不同配置文件使前台应用Smarty缓存后台不应用
- smarty缓存的应用
- Smarty模板缓存应用
- CI 里面整合Smarty
- ci smarty 整合
- ci smarty 整合
- CI轻松整合smarty
- smarty中配置文件的应用
- smarty安装和应用
- Smarty的缓存与应用
- 运用smarty后台搭建和前台显示~
- Smarty应用
- 应用smarty
- smarty应用
- Smarty应用
- Smarty 下前后台集成
- ci整合Smarty-3.1.18
- CI框架整合smarty模板
- HDU 2008 - 数值统计
- 通过setImageURI设置网络上面的图片
- chrome设置:新标签页打开指定网页
- HDU 2009 - 求数列的和
- pio应用excel,word转html
- CI和Smarty整合并且前后台加载不同配置文件使前台应用Smarty缓存后台不应用
- Fibnacci序列中的第20个数(递归)
- yii -- 路由
- python操作MySQL数据库
- 对yii的初理解
- HDU 2010 - 水仙花数
- php所有版本变化情况
- bootstrap从2.*到3.0的一些class变化
- HDU 4750 Count The Pairs