ThinkPHP3.2.3 page static implementation method

小云云
Release: 2023-03-19 18:48:02
Original
1616 people have browsed it

本文主要给大家介绍关于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


'配置值'
 'INDEX_MOD'=>1,//首页模式 0-动态模式 1-静态模式
 'INDEX_HTML_FILE'=>__ROOT__.'Application/Home/View/Index/index_html.html',//静态首页地址
);
Copy after login

/Application/Home/Controller/IndexController.php


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();
 }
}
Copy after login

模版文件 /Application/Home/View/Index/index_php.php





 
 Document

{$vo.cat_name}
Copy after login

在执行 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;
 }
Copy after login

其中 Storage 类在 /ThinkPHP/Library/Think/Storage.class.php



// +----------------------------------------------------------------------
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);
  }
 }
}
Copy after login

默认的文件类型是 File,所以实例化的类的地址在 /ThinkPHP/Library/Think/Storage/Driver/File.class.php



// +----------------------------------------------------------------------
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];
 }
}
Copy after login

可以看到 get 和 put 方法所使用的方法是 file_get_contents() file_put_contents()

相关推荐:

html实现页面静态化的案例

php实现页面静态化视频教程资料推荐

php页面静态化实现的几种分享

The above is the detailed content of ThinkPHP3.2.3 page static implementation method. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!