本文主要给大家介绍关于thinkphp 3.2.3实现页面静态化功能的方法,我们首先会和大家介绍一下页面静态化的几种实现方式,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面来一起看看吧。
大家都知道php 的页面静态化有多种实现方式,比如使用输出缓冲(output buffering),该种方式是把数据缓存在 php 的缓冲区(内存)中,下一次取数据时直接从缓冲区中读取数据,从而避免了脚本的编译和访问数据库等过程;另一种方式是直接生成静态的 html 文件,使用文件读写函数来实现,一些内容不经常改动的页面可以使用静态页面,访客访问到的页面就是真实的 html 页面,一些常见的 cms 会使用该种方法。
以第二种方法为例,参考 dedecms 5.7 的静态化功能,在 thinkphp 3.2.3 下实现该方法。由于 thinkphp 是单入口系统,而且每一个页面都要对应控制器中的某个方法,因此不能直接把静态文件的地址作为实际访问的地址,而是需要在控制器中以模版加载的方式读取静态文件。
首页静态化的实现
在 dedecms 5.7 中,可以生成静态的首页、栏目页和文章页。其中首页的生成在后台的“生成”栏目进行设置,包括模板的选择、首页静态文件的存放路径以及首页模式(使用动态首页还是静态首页),dedecms 还专门为首页的设置设计了一张表 dede_homepageset,包含的字段包括 templet(模板位置)、position(首页静态文件的路径)、showmod(首页模式),通过在后台进行设置与生成,来控制网站首页使用动态首页还是静态首页,用到的核心文件是 \dede\makehtml_homepage.php。
流程大致是:
① 在后台选择生成静态页面时,通过表单向 makehtml_homepage.php 发送请求,参数包括 dede_homepageset 的所有字段
② 根据传递参数中的 templet、position、showmod 更新 dede_homepageset 表
③ 如果 showmod 是使用静态,加载模板,把模板保存为静态文件。使用的方法是 fopen(),fwrite() 和 fclose(),非常简单
④ 生成了静态页面之后,访客访问的就直接是静态的 index.html,如果首页发生了改变,则手动在后台重新生成一下首页
在 thinkphp 中可以这样设计:
config.php
<?php
return array(
//'配置项'=>'配置值'
'index_mod'=>1,//首页模式 0-动态模式 1-静态模式
'index_html_file'=>__root__.'application/home/view/index/index_html.html',//静态首页地址
);
/application/home/controller/indexcontroller.php
<?php
namespace home\controller;
use think\controller;
class indexcontroller extends controller {
//首页
public function index() {
if(1 == c('index_mod')) {
//静态
$this->display('index_html');
} else {
//动态
$list = m('category')->select();
$this->assign('list', $list);
$this->display('index_php');
}
}
//根据动态首页的内容生成静态页面
public function makehtml_homepage() {
$homepage = 'http://'.$_server['http_host'].u('home/index/index_php');
$content = @file_get_contents($homepage);
file_put_contents(c('index_html_file'), $content);
}
//动态首页数据
public function index_php() {
c('index_mod', 0);
$this->index();
}
}
模版文件 /application/home/view/index/index_php.php
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>document</title>
</head>
<body>
<volist name="list" id="vo">
{$vo.cat_name}<br />
</volist>
</body>
</html>
在执行 http://servername/home/index/makehtml_homepage ,即手动生成静态首页后,在 /application/home/view/index/ 路径下生成了静态文件:index_html.html,根据配置文件中设置的 index_mode 为静态,访问 http://servername 实际访问的就是新生成的静态文件。
thinkphp 也自带了生成静态文件的方法 buildhtml,使用方法是 buildhtml('生成的静态文件名称', '生成的静态文件路径', '指定要调用的模板文件');
方法在 /thinkphp/library/think/controller.class.php,line 86:
/**
* 创建静态页面
* @access protected
* @htmlfile 生成的静态文件名称
* @htmlpath 生成的静态文件路径
* @param string $templatefile 指定要调用的模板文件
* 默认为空 由系统自动定位模板文件
* @return string
*/
protected function buildhtml($htmlfile='',$htmlpath='',$templatefile='') {
$content = $this->fetch($templatefile);
$htmlpath = !empty($htmlpath)?$htmlpath:html_path;
$htmlfile = $htmlpath.$htmlfile.c('html_file_suffix');
storage::put($htmlfile,$content,'html');
return $content;
}
其中 storage 类在 /thinkphp/library/think/storage.class.php
<?php
// +----------------------------------------------------------------------
// | topthink [ we can do it just think ]
// +----------------------------------------------------------------------
// | copyright (c) 2013 http://topthink.com all rights reserved.
// +----------------------------------------------------------------------
// | licensed ( http://www.apache.org/licenses/license-2.0 )
// +----------------------------------------------------------------------
// | author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
namespace think;
// 分布式文件存储类
class storage {
/**
* 操作句柄
* @var string
* @access protected
*/
static protected $handler ;
/**
* 连接分布式文件系统
* @access public
* @param string $type 文件类型
* @param array $options 配置数组
* @return void
*/
static public function connect($type='file',$options=array()) {
$class = 'think\\storage\\driver\\'.ucwords($type);
self::$handler = new $class($options);
}
static public function __callstatic($method,$args){
//调用缓存驱动的方法
if(method_exists(self::$handler, $method)){
return call_user_func_array(array(self::$handler,$method), $args);
}
}
}
默认的文件类型是 file,所以实例化的类的地址在 /thinkphp/library/think/storage/driver/file.class.php
<?php
// +----------------------------------------------------------------------
// | topthink [ we can do it just think ]
// +----------------------------------------------------------------------
// | copyright (c) 2013 http://topthink.com all rights reserved.
// +----------------------------------------------------------------------
// | licensed ( http://www.apache.org/licenses/license-2.0 )
// +----------------------------------------------------------------------
// | author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
namespace think\storage\driver;
use think\storage;
// 本地文件写入存储类
class file extends storage{
private $contents=array();
/**
* 架构函数
* @access public
*/
public function __construct() {
}
/**
* 文件内容读取
* @access public
* @param string $filename 文件名
* @return string
*/
public function read($filename,$type=''){
return $this->get($filename,'content',$type);
}
/**
* 文件写入
* @access public
* @param string $filename 文件名
* @param string $content 文件内容
* @return boolean
*/
public function put($filename,$content,$type=''){
$dir = dirname($filename);
if(!is_dir($dir))
mkdir($dir,0755,true);
if(false === file_put_contents($filename,$content)){
e(l('_storage_write_error_').':'.$filename);
}else{
$this->contents[$filename]=$content;
return true;
}
}
/**
* 文件追加写入
* @access public
* @param string $filename 文件名
* @param string $content 追加的文件内容
* @return boolean
*/
public function append($filename,$content,$type=''){
if(is_file($filename)){
$content = $this->read($filename,$type).$content;
}
return $this->put($filename,$content,$type);
}
/**
* 加载文件
* @access public
* @param string $filename 文件名
* @param array $vars 传入变量
* @return void
*/
public function load($_filename,$vars=null){
if(!is_null($vars))
extract($vars, extr_overwrite);
include $_filename;
}
/**
* 文件是否存在
* @access public
* @param string $filename 文件名
* @return boolean
*/
public function has($filename,$type=''){
return is_file($filename);
}
/**
* 文件删除
* @access public
* @param string $filename 文件名
* @return boolean
*/
public function unlink($filename,$type=''){
unset($this->contents[$filename]);
return is_file($filename) ? unlink($filename) : false;
}
/**
* 读取文件信息
* @access public
* @param string $filename 文件名
* @param string $name 信息名 mtime或者content
* @return boolean
*/
public function get($filename,$name,$type=''){
if(!isset($this->contents[$filename])){
if(!is_file($filename)) return false;
$this->contents[$filename]=file_get_contents($filename);
}
$content=$this->contents[$filename];
$info = array(
'mtime' => filemtime($filename),
'content' => $content
);
return $info[$name];
}
}
可以看到 get 和 put 方法所使用的方法是 file_get_contents() 和 file_put_contents() 。
相关推荐:
html实现页面静态化的案例
php实现页面静态化视频教程资料推荐
php页面静态化实现的几种分享
以上就是thinkphp3.2.3页面静态化实现方法的详细内容。