当前位置:Gxlcms > php框架 > ThinkPHP 3.2.3实现页面静态化功能的方法详解

ThinkPHP 3.2.3实现页面静态化功能的方法详解

时间:2021-07-01 10:21:17 帮助过:42人阅读

前言

大家都知道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

  1. <?php
  2. return array(
  3. //'配置项'=>'配置值'
  4. 'INDEX_MOD'=>1,//首页模式 0-动态模式 1-静态模式
  5. 'INDEX_HTML_FILE'=>__ROOT__.'Application/Home/View/Index/index_html.html',//静态首页地址
  6. );

/Application/Home/Controller/IndexController.php

  1. <?php
  2. namespace Home\Controller;
  3. use Think\Controller;
  4. class IndexController extends Controller {
  5. //首页
  6. public function index() {
  7. if(1 == C('INDEX_MOD')) {
  8. //静态
  9. $this->display('index_html');
  10. } else {
  11. //动态
  12. $list = M('category')->select();
  13. $this->assign('list', $list);
  14. $this->display('index_php');
  15. }
  16. }
  17. //根据动态首页的内容生成静态页面
  18. public function makehtml_homepage() {
  19. $homepage = 'http://'.$_SERVER['HTTP_HOST'].U('Home/Index/index_php');
  20. $content = @file_get_contents($homepage);
  21. file_put_contents(C('INDEX_HTML_FILE'), $content);
  22. }
  23. //动态首页数据
  24. public function index_php() {
  25. C('INDEX_MOD', 0);
  26. $this->index();
  27. }
  28. }

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

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Document</title>
  6. </head>
  7. <body>
  8. <volist name="list" id="vo">
  9. {$vo.cat_name}<br />
  10. </volist>
  11. </body>
  12. </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:

  1. /**
  2. * 创建静态页面
  3. * @access protected
  4. * @htmlfile 生成的静态文件名称
  5. * @htmlpath 生成的静态文件路径
  6. * @param string $templateFile 指定要调用的模板文件
  7. * 默认为空 由系统自动定位模板文件
  8. * @return string
  9. */
  10. protected function buildHtml($htmlfile='',$htmlpath='',$templateFile='') {
  11. $content = $this->fetch($templateFile);
  12. $htmlpath = !empty($htmlpath)?$htmlpath:HTML_PATH;
  13. $htmlfile = $htmlpath.$htmlfile.C('HTML_FILE_SUFFIX');
  14. Storage::put($htmlfile,$content,'html');
  15. return $content;
  16. }

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

  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | TOPThink [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2013 http://topthink.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. namespace Think;
  12. // 分布式文件存储类
  13. class Storage {
  14. /**
  15. * 操作句柄
  16. * @var string
  17. * @access protected
  18. */
  19. static protected $handler ;
  20. /**
  21. * 连接分布式文件系统
  22. * @access public
  23. * @param string $type 文件类型
  24. * @param array $options 配置数组
  25. * @return void
  26. */
  27. static public function connect($type='File',$options=array()) {
  28. $class = 'Think\\Storage\\Driver\\'.ucwords($type);
  29. self::$handler = new $class($options);
  30. }
  31. static public function __callstatic($method,$args){
  32. //调用缓存驱动的方法
  33. if(method_exists(self::$handler, $method)){
  34. return call_user_func_array(array(self::$handler,$method), $args);
  35. }
  36. }
  37. }

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

  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | TOPThink [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2013 http://topthink.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. namespace Think\Storage\Driver;
  12. use Think\Storage;
  13. // 本地文件写入存储类
  14. class File extends Storage{
  15. private $contents=array();
  16. /**
  17. * 架构函数
  18. * @access public
  19. */
  20. public function __construct() {
  21. }
  22. /**
  23. * 文件内容读取
  24. * @access public
  25. * @param string $filename 文件名
  26. * @return string
  27. */
  28. public function read($filename,$type=''){
  29. return $this->get($filename,'content',$type);
  30. }
  31. /**
  32. * 文件写入
  33. * @access public
  34. * @param string $filename 文件名
  35. * @param string $content 文件内容
  36. * @return boolean
  37. */
  38. public function put($filename,$content,$type=''){
  39. $dir = dirname($filename);
  40. if(!is_dir($dir))
  41. mkdir($dir,0755,true);
  42. if(false === file_put_contents($filename,$content)){
  43. E(L('_STORAGE_WRITE_ERROR_').':'.$filename);
  44. }else{
  45. $this->contents[$filename]=$content;
  46. return true;
  47. }
  48. }
  49. /**
  50. * 文件追加写入
  51. * @access public
  52. * @param string $filename 文件名
  53. * @param string $content 追加的文件内容
  54. * @return boolean
  55. */
  56. public function append($filename,$content,$type=''){
  57. if(is_file($filename)){
  58. $content = $this->read($filename,$type).$content;
  59. }
  60. return $this->put($filename,$content,$type);
  61. }
  62. /**
  63. * 加载文件
  64. * @access public
  65. * @param string $filename 文件名
  66. * @param array $vars 传入变量
  67. * @return void
  68. */
  69. public function load($_filename,$vars=null){
  70. if(!is_null($vars))
  71. extract($vars, EXTR_OVERWRITE);
  72. include $_filename;
  73. }
  74. /**
  75. * 文件是否存在
  76. * @access public
  77. * @param string $filename 文件名
  78. * @return boolean
  79. */
  80. public function has($filename,$type=''){
  81. return is_file($filename);
  82. }
  83. /**
  84. * 文件删除
  85. * @access public
  86. * @param string $filename 文件名
  87. * @return boolean
  88. */
  89. public function unlink($filename,$type=''){
  90. unset($this->contents[$filename]);
  91. return is_file($filename) ? unlink($filename) : false;
  92. }
  93. /**
  94. * 读取文件信息
  95. * @access public
  96. * @param string $filename 文件名
  97. * @param string $name 信息名 mtime或者content
  98. * @return boolean
  99. */
  100. public function get($filename,$name,$type=''){
  101. if(!isset($this->contents[$filename])){
  102. if(!is_file($filename)) return false;
  103. $this->contents[$filename]=file_get_contents($filename);
  104. }
  105. $content=$this->contents[$filename];
  106. $info = array(
  107. 'mtime' => filemtime($filename),
  108. 'content' => $content
  109. );
  110. return $info[$name];
  111. }
  112. }

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

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对脚本之家的支持。

人气教程排行