This article mainly introduces the thinkphp 3.2.3 implementation of the page static function of the method, has a certain reference value, now share to everyone, the need for friends can refer to
Page static is a feature that we often need in the development of the site, the following this article is mainly about thinkphp 3.2.3 implementation of the page static function of the method, the text through the sample code introduced in very detailed, the need for friends can refer to, the following to see together.
Objective
We all know that there are several implementations of page static in PHP, such as the use of output buffering (buffering), which is to cache the data in PHP buffer (memory), the next time the data is fetched directly from the buffer to read the data, thus avoiding the compilation of scripts and access to the database and other processes Another way is to directly generate static HTML files, using the file read and write functions, some of the pages are not often changed to use static pages, visitors to the page is the real HTML page, some common CMS will use this method.
Taking the second method as an example, refer to the static function of Dedecms 5.7 and implement the method under Thinkphp 3.2.3. Since thinkphp is a single-entry system, and each page has to correspond to a method in the controller, it is not possible to directly use the address of the static file as the actual access address, but rather need to read the static file in the controller in the form of the template loading.
The realization of homepage static
In Dedecms 5.7, you can generate static home page, column pages, and article pages. Where the home page generated in the background of the "Generate" column to set up, including template selection, home static file storage path and home mode (using dynamic home or static home), DEDECMS also dedicated to the first page of the settings designed a table Dede_homepageset, contains the fields include Templet (template location), position (the path of the home static file), Showmod (home mode), through the background to set up and build, to control the home page using dynamic home or static home page, the core file used is \dede\makehtml_ homepage.php.
The process is roughly:
① send a request through the table one-way makehtml_homepage.php when generating a static page in the background, parameters include all fields of Dede_homepageset
② Update Dede_homepageset table based on Templet, position, showmod in pass parameters
③ if Showmod is using static, load the template and save the template as a static file. The method used is fopen (), fwrite () and fclose (), very simple
④ generated a static page, the guest access is directly static index.html, if the first page has changed, then manually in the background to regenerate the first page
This can be designed in thinkphp:
config.php
<?phpreturn Array (//' Config item ' + ' config value ' = ' index_mod ' =>1,//home mode 0-Dynamic mode 1-static mode ' Index_html_file ' =>__root__. ' Application/home/view/index/index_html.html ',//static home address);
/application/home/controller/indexcontroller.php
<?phpnamespace home\controller;use Think\controller;class Indexcontroller extends Controller { //home public function index () { if (1 = = C (' index_mod ')) { //static $this->display (' index_html '); } else { //dynamic $list = M (' category ')->select (); $this->assign (' list ', $list); $this->display (' index_php '); } } //Generate static pages based on the contents of the Dynamic Home page 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); } //Dynamic Home data Public Function index_php () { C (' Index_mod ', 0); $this->index (); }}
Template file/application/home/view/index/index_php.php
<! DOCTYPE html>
Static files are generated under the/application/home/view/index/path after executing http://ServerName/Home/Index/makehtml_homepage, that is, manually generating a static home page: Index_ Html.html, the Index_mode that is set in the configuration file is static, and the access Http://ServerName actually accesses the newly generated static file.
Thinkphp also comes with the method of generating the static file buildhtml, using the method is buildhtml (' generated static file name ', ' generated static file path ', ' Specify the template file to invoke ');
Method in/thinkphp/library/think/controller.class.php,line 86:
/** * Create static page * @access protected * @htmlfile generated static file name * @htmlpath generated static file path * @param string $ templatefile specifies the template file to invoke * default is empty by the system automatically locates the template file * @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::p ut ($htmlfile, $content, ' html '); return $content; }
Where the Storage class is in/thinkphp/library/think/storage.class.php
<?php//+----------------------------------------------------------------------//| Topthink [WE CAN do IT JUST THINK]//+----------------------------------------------------------------------//| Copyright (c) http://topthink.com All rights reserved.//+------------------------------------------------------ ----------------// | Licensed (http://www.apache.org/licenses/LICENSE-2.0)//+------------------------------------------------------- ---------------// | author:liu21st <liu21st@gmail.com>//+-------------------------------------------------------------------- --namespace think;//Distributed File storage classes class Storage {/** * operation handle * @var String * @access protected */static protected $handler ; /** * Connection to Distributed File system * @access public * @param string $type file type * @param array $options configuration arrays * @return void */static PU Blic function Connect ($type = ' File ', $options =array ()) {$class = ' think\\storage\\driver\\ '. Ucwords ($type); Self:: $handler = new $class ($options); } static Public FuNction __callstatic ($method, $args) {//Call Cache-driven method if (Method_exists (self:: $handler, $method)) {return call_user_func_ Array (self:: $handler, $method), $args); } }}
The default file type is files, so the address of the instantiated class is/thinkphp/library/think/storage/driver/file.class.php
<?php//+----------------------------------------------------------------------//| Topthink [WE CAN do IT JUST THINK]//+----------------------------------------------------------------------//| Copyright (c) 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;//Local file is written to the storage class class files extends storage{private $contents =array (); * * Schema function * @access public */Public function __construct () {}/** * file contents Read * @access public * @param string $filen AME File name * @return String */Public function read ($filename, $type = ') {return $this->get ($filename, ' content ', $type) ; }/** * File written * @access public * @param string $filename File name * @param string $content file Contents * @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; }}/** * file append write * @access public * @param string $filename filename * @param string $content appended file contents * @return Boolean */Public function append ($filename, $content, $type = ") {if (Is_file ($filename)) {$content = $this->read ($filename, $ Type). $content; } return $this->put ($filename, $content, $type); /** * Load File * @access public * @param string $filename file name * @param array $vars pass in variable * @return void * * Public fun ction load ($_filename, $vars =null) {if (!is_null ($vars)) Extract ($vars, extr_overwrite); Include $_filename; }/** * File exists * @access public * @param string $filename file name * @return Boolean */PubLic function has ($filename, $type = ') {return is_file ($filename);}/** * File Delete * @access public * @param string $filena Me file name * @return Boolean */Public Function unlink ($filename, $type = ") {unset ($this->contents[$filename]); Return Is_file ($filename)? Unlink ($filename): false; /** * Read File information * @access public * @param string $filename file name * @param string $name information name mtime or content * @return bool EAN */Public function get ($filename, $name, $type = ") {if (!isset ($this->contents[$filename])) {if (!is_file ($filena Me)) return false; $this->contents[$filename]=file_get_contents ($filename); } $content = $this->contents[$filename]; $info = Array (' mtime ' = = Filemtime ($filename), ' content ' = $content); return $info [$name]; }}
You can see how the get and put methods are used file_get_contents()
file_put_contents()
.