亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

ThinkPHP 3.2.3實(shí)現(xiàn)頁面靜態(tài)化功能的方法詳解

 更新時(shí)間:2017年08月03日 12:02:48   作者:黃棣-dee  
頁面靜態(tài)化是我們?cè)陂_發(fā)網(wǎng)站的時(shí)候經(jīng)常需要的一個(gè)功能,下面這篇文章主要給大家介紹了關(guān)于ThinkPHP 3.2.3實(shí)現(xiàn)頁面靜態(tài)化功能的方法,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來一起看看吧。

前言

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

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

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

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

流程大致是:

① 在后臺(tái)選擇生成靜態(tài)頁面時(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(),非常簡單

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

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

config.php

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

/Application/Home/Controller/IndexController.php

<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
 
 //首頁
 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)首頁的內(nèi)容生成靜態(tài)頁面
 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)首頁數(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)首頁后,在 /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)頁面
  * @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()

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對(duì)腳本之家的支持。

相關(guān)文章

  • 詳解HTTP Cookie狀態(tài)管理機(jī)制

    詳解HTTP Cookie狀態(tài)管理機(jī)制

    cookie 最早是網(wǎng)景公司的雇員 Lou Montulli 在1993年3月發(fā)明,后被 W3C 采納,目前 cookie 已經(jīng)成為標(biāo)準(zhǔn),所有的主流瀏覽器如 IE、Chrome、Firefox、Opera 等都支持
    2016-01-01
  • PHP字符串的連接的簡單實(shí)例

    PHP字符串的連接的簡單實(shí)例

    這篇文章主要介紹了PHP字符串的連接的簡單實(shí)例,有需要的朋友可以參考一下
    2013-12-12
  • PHP語法自動(dòng)檢查的Vim插件

    PHP語法自動(dòng)檢查的Vim插件

    Vim是從 vi 發(fā)展出來的一個(gè)文本編輯器。代碼補(bǔ)完、編譯及錯(cuò)誤跳轉(zhuǎn)等方便編程的功能特別豐富,在程序員中被廣泛使用。和Emacs并列成為類Unix系統(tǒng)用戶最喜歡的編輯器。
    2014-08-08
  • PHP智能識(shí)別收貨地址信息實(shí)例

    PHP智能識(shí)別收貨地址信息實(shí)例

    今天小編就為大家分享一篇關(guān)于PHP智能識(shí)別收貨地址信息實(shí)例,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2019-01-01
  • Yii2使用小技巧之通過 Composer 添加 FontAwesome 字體資源

    Yii2使用小技巧之通過 Composer 添加 FontAwesome 字體資源

    前天幫同事改個(gè)十年前的網(wǎng)站 bug,頁面上一堆 include require 不禁讓人抱頭痛哭。看到 V2EX 上的討論說,寫 PHP 不用框架等同于耍流氓。Yii Framework 是我使用了 2 年多的 PHP 框架,器大活好,皮實(shí)耐操。 Yii2 還在 Beta 中,不過不影響拿來預(yù)研。
    2014-06-06
  • 淺談PHP中try{}catch{}的使用方法

    淺談PHP中try{}catch{}的使用方法

    下面小編就為大家?guī)硪黄獪\談PHP中try{}catch{}的使用方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2016-12-12
  • Thinkphp5 微信公眾號(hào)token驗(yàn)證不成功的原因及解決方法

    Thinkphp5 微信公眾號(hào)token驗(yàn)證不成功的原因及解決方法

    下面小編就為大家?guī)硪黄猅hinkphp5 微信公眾號(hào)token驗(yàn)證不成功的原因及解決方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-11-11
  • Laravel 連接(Join)示例

    Laravel 連接(Join)示例

    今天小編就為大家分享一篇Laravel 連接(Join)示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-10-10
  • tp5(thinkPHP5)框架數(shù)據(jù)庫Db增刪改查常見操作總結(jié)

    tp5(thinkPHP5)框架數(shù)據(jù)庫Db增刪改查常見操作總結(jié)

    這篇文章主要介紹了tp5(thinkPHP5)框架數(shù)據(jù)庫Db增刪改查常見操作,結(jié)合實(shí)例形式總結(jié)分析了thinkPHP5框架數(shù)據(jù)庫的增刪改查常見操作技巧與相關(guān)注意事項(xiàng),需要的朋友可以參考下
    2019-01-01
  • 淺談laravel-admin的sortable和orderby使用問題

    淺談laravel-admin的sortable和orderby使用問題

    今天小編就為大家分享一篇淺談laravel-admin的sortable和orderby使用問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-10-10

最新評(píng)論