溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

ThinkPHP3.2.3怎么實(shí)現(xiàn)頁(yè)面靜態(tài)化功能

發(fā)布時(shí)間:2021-07-08 09:03:43 來源:億速云 閱讀:136 作者:小新 欄目:開發(fā)技術(shù)

這篇文章將為大家詳細(xì)講解有關(guān)ThinkPHP3.2.3怎么實(shí)現(xiàn)頁(yè)面靜態(tài)化功能,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

前言

大家都知道PHP 的頁(yè)面靜態(tài)化有多種實(shí)現(xiàn)方式,比如使用輸出緩沖(output buffering),該種方式是把數(shù)據(jù)緩存在 PHP 的緩沖區(qū)(內(nèi)存)中,下一次取數(shù)據(jù)時(shí)直接從緩沖區(qū)中讀取數(shù)據(jù),從而避免了腳本的編譯和訪問數(shù)據(jù)庫(kù)等過程;另一種方式是直接生成靜態(tài)的 HTML 文件,使用文件讀寫函數(shù)來實(shí)現(xiàn),一些內(nèi)容不經(jīng)常改動(dòng)的頁(yè)面可以使用靜態(tài)頁(yè)面,訪客訪問到的頁(yè)面就是真實(shí)的 HTML 頁(yè)面,一些常見的 CMS 會(huì)使用該種方法。

以第二種方法為例,參考 DedeCMS 5.7 的靜態(tài)化功能,在 ThinkPHP 3.2.3 下實(shí)現(xiàn)該方法。由于 ThinkPHP 是單入口系統(tǒng),而且每一個(gè)頁(yè)面都要對(duì)應(yīng)控制器中的某個(gè)方法,因此不能直接把靜態(tài)文件的地址作為實(shí)際訪問的地址,而是需要在控制器中以模版加載的方式讀取靜態(tài)文件。

首頁(yè)靜態(tài)化的實(shí)現(xiàn)

在 DedeCMS 5.7 中,可以生成靜態(tài)的首頁(yè)、欄目頁(yè)和文章頁(yè)。其中首頁(yè)的生成在后臺(tái)的“生成”欄目進(jìn)行設(shè)置,包括模板的選擇、首頁(yè)靜態(tài)文件的存放路徑以及首頁(yè)模式(使用動(dòng)態(tài)首頁(yè)還是靜態(tài)首頁(yè)),DedeCMS 還專門為首頁(yè)的設(shè)置設(shè)計(jì)了一張表 dede_homepageset,包含的字段包括 templet(模板位置)、position(首頁(yè)靜態(tài)文件的路徑)、showmod(首頁(yè)模式),通過在后臺(tái)進(jìn)行設(shè)置與生成,來控制網(wǎng)站首頁(yè)使用動(dòng)態(tài)首頁(yè)還是靜態(tài)首頁(yè),用到的核心文件是 \dede\makehtml_homepage.php。

流程大致是:

① 在后臺(tái)選擇生成靜態(tài)頁(yè)面時(shí),通過表單向 makehtml_homepage.php 發(fā)送請(qǐng)求,參數(shù)包括 dede_homepageset 的所有字段

② 根據(jù)傳遞參數(shù)中的 templet、position、showmod 更新 dede_homepageset 表

③ 如果 showmod 是使用靜態(tài),加載模板,把模板保存為靜態(tài)文件。使用的方法是 fopen(),fwrite() 和 fclose(),非常簡(jiǎn)單

④ 生成了靜態(tài)頁(yè)面之后,訪客訪問的就直接是靜態(tài)的 index.html,如果首頁(yè)發(fā)生了改變,則手動(dòng)在后臺(tái)重新生成一下首頁(yè)

在 ThinkPHP 中可以這樣設(shè)計(jì):

config.php

<?php
return array(
 //'配置項(xiàng)'=>'配置值'
 'INDEX_MOD'=>1,//首頁(yè)模式 0-動(dòng)態(tài)模式 1-靜態(tài)模式
 'INDEX_HTML_FILE'=>__ROOT__.'Application/Home/View/Index/index_html.html',//靜態(tài)首頁(yè)地址
);

/Application/Home/Controller/IndexController.php

<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
 
 //首頁(yè)
 public function index() {
  if(1 == C('INDEX_MOD')) {
   //靜態(tài)
   $this->display('index_html');
  } else {
   //動(dòng)態(tài)
   $list = M('category')->select();
   $this->assign('list', $list);
   $this->display('index_php');
  }
 }
 
 //根據(jù)動(dòng)態(tài)首頁(yè)的內(nèi)容生成靜態(tài)頁(yè)面
 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);
 }
 
 //動(dòng)態(tài)首頁(yè)數(shù)據(jù)
 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>

在執(zhí)行 http://ServerName/Home/Index/makehtml_homepage ,即手動(dòng)生成靜態(tài)首頁(yè)后,在 /Application/Home/View/Index/ 路徑下生成了靜態(tài)文件:index_html.html,根據(jù)配置文件中設(shè)置的 INDEX_MODE 為靜態(tài),訪問 http://ServerName 實(shí)際訪問的就是新生成的靜態(tài)文件。

ThinkPHP 也自帶了生成靜態(tài)文件的方法 buildHtml,使用方法是 buildHtml('生成的靜態(tài)文件名稱', '生成的靜態(tài)文件路徑', '指定要調(diào)用的模板文件');

方法在 /ThinkPHP/Library/Think/Controller.class.php,Line 86:

/**
  * 創(chuàng)建靜態(tài)頁(yè)面
  * @access protected
  * @htmlfile 生成的靜態(tài)文件名稱
  * @htmlpath 生成的靜態(tài)文件路徑
  * @param string $templateFile 指定要調(diào)用的模板文件
  * 默認(rèn)為空 由系統(tǒng)自動(dòng)定位模板文件
  * @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;
// 分布式文件存儲(chǔ)類
class Storage {

 /**
  * 操作句柄
  * @var string
  * @access protected
  */
 static protected $handler ;

 /**
  * 連接分布式文件系統(tǒng)
  * @access public
  * @param string $type 文件類型
  * @param array $options 配置數(shù)組
  * @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){
  //調(diào)用緩存驅(qū)動(dòng)的方法
  if(method_exists(self::$handler, $method)){
   return call_user_func_array(array(self::$handler,$method), $args);
  }
 }
}

默認(rèn)的文件類型是 File,所以實(shí)例化的類的地址在 /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;
// 本地文件寫入存儲(chǔ)類
class File extends Storage{

 private $contents=array();

 /**
  * 架構(gòu)函數(shù)
  * @access public
  */
 public function __construct() {
 }

 /**
  * 文件內(nèi)容讀取
  * @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 文件內(nèi)容
  * @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 追加的文件內(nèi)容
  * @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()

關(guān)于“ThinkPHP3.2.3怎么實(shí)現(xiàn)頁(yè)面靜態(tài)化功能”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI