A static php class for dry pages-php Tutorial

Source: Internet
Author: User
A static php class for pages & lt ;? PhpnamespaceCommon; *** function: static page creation and deletion * creation: when and only when a page needs to be static and not yet static. * Delete: only when a page is static and needs to be static again. ** Author: Guo Junzhou ** Note: This class is based on ThinkPHP3.2, or other php class with "one static page entry"
 _ Sh = StaticHtml: getInstance (); *} * function _ destruct () {* $ this->__ sh-> _ static (); *} ***/class StaticHtml {private static $ _ instance = null;/* single-instance mode, reference */private $ _ needStatic = false; /* do you need to statically convert it */private $ _ needDeleteStatic = false;/* do you need to delete the static page */private $ _ hasStaticed = true; /* whether the static page exists */private $ _ controller = null;/* the currently accessed controller */private $ _ action = null;/* the currently accessed controller is The Accessed action * // private $ _ staticAgain = false;/* after deleting the static file, whether to immediately update [note] request again] */private $ _ save_path = null; /* path of the static file to be created or deleted */private $ _ conf = array (/* This File defines some static file storage methods */'files _ per_directory '=> 100/* This value cannot be modified, unless you want to delete all existing static files and cache them again */); private $ _ staticList = array (/* This array, define the action * // 'base' => array (/* Base is Controller name * // 'AAA' => array (/* Base is controller name (/ * aaa is the action name. * // 'SAVE _ path' => '/StaticHtml/Base/aaa /', /* save_path is the root path for generating static files * // 'static _ base' => 'id', and/* static_base is the "basis" for generating static files ". We recommend that you set the database's primary_key * // 'Alias' => 'AAA'/* name of the static file, and the negative value is 1.html *///)//) 'mynote' => array ('Look _ note' => array ('SAVE _ path' => '/StaticHtml/Mynotes/look_notes /', 'static _ base' => 'id', 'Alias' => 'note '), 'Add _ personal_notes '=> array ('SAVE _ path' =>'/StaticHtml/Mynotes/', 'Alias' => 'note-add ')), 'resource' => array ('allresource' => array ('SAVE _ path' => '/StaticHtml/Resource/', 'Alias' => 'Allresource'), 'resource _ add' => array ('SAVE _ path' => '/StaticHtml/resource /', 'Alias' => 'resourceadd ')), 'Thing '=> array ('suggestion _ of_lecture' => array ('SAVE _ path' => '/StaticHtml/Lecture /', 'Alias' => 'votelecture '), 'passwordfix' => array ('index' => array ('SAVE _ path' =>'/StaticHtml/Information /', 'Alias' => 'updatepassword'), 'information' => array ('index' => array ('sa Ve_path '=>'/StaticHtml/Information/', 'static _ base' => 'User _ id', 'Alias' => 'info ')), 'courseinfo' => array ('course _ Show' => array ('SAVE _ path' => '/StaticHtml/Information /', 'static _ base' => 'User _ id', 'Alias' => 'course'); private $ _ deleteList = array (/* This array, define the action * // 'base' => array (/* Base is Controller name * // 'BBB '=> array (/* bbb is action name * /// 'SAVE _ path' => '/StaticHtml/Base/aaa /', /* save_path is the root path of the static file to be deleted * // 'static _ base' => 'id ', /* static_base is the "basis" for determining the static file path ". We recommend that you set the database's primary_key * // 'Alias' => 'AAA'/* name of the static file, and the negative value is 1.html *///)//) 'mynote' => array ('edits _ note' => array ('SAVE _ path' => '/StaticHtml/Mynotes/look_notes /', 'static _ base' => 'id', 'Alias' => 'note ')), 'info' => array ('SAVE _ user_info '=> array ('SAVE _ path' =>'/StaticHtml/Information /', 'static _ base' => 'User _ id', 'Alias' => 'Information'), 'courseinfo' => array ('course _ upd Ate '=> array ('SAVE _ path' =>'/StaticHtml/Information/', 'static _ base' => 'User _ id ', 'Alias' => 'course'); private function _ construct () {$ this-> needStatic (); /* determine whether the request needs to be static */$ this-> hasStaticed ();/* determine whether the request already has a static page */$ this-> needDeleteStatic (); /* determine whether to delete some static pages in this request */}/* Determine the storage path of the static files to be deleted */private function needDeleteStatic () {if ($ this-> _ needDeleteStatic) {$ save_path = $ this-> getS AvePath ($ this-> _ deleteList [$ this-> _ controller] [$ this-> _ action]); $ this-> _ hasStaticed = false; if (file_exists (ROOT_PATH. $ save_path) {$ this-> _ hasStaticed = true ;} // $ this-> _ staticAgain = $ this-> _ deleteList [$ this-> _ controller] [$ this-> _ action] ['visitagain ']; $ this-> _ save_path = ROOT_PATH. $ save_path ;}}/* obtains the unique value of this class. it is instantiated. */public static function getInstance () {if (! (Self: $ _ instance instanceof self) {self: $ _ instance = new self ();} return self ::$ _ instance ;} /* determine whether a static file exists */private function hasStaticed () {if ($ this-> _ needStatic) {$ save_path = $ this-> getSavePath ($ this-> _ staticList [$ this-> _ controller] [$ this-> _ action]); if (! File_exists (ROOT_PATH. $ save_path) {$ this-> _ hasStaticed = false; ob_start ();} else {header ("location: http ://". $ _ SERVER ['http _ host']. dirname ($ _ SERVER ['script _ name']). $ save_path);} $ this-> _ save_path = ROOT_PATH. $ save_path ;}}/* obtain the path of the static file to be generated or deleted in this request */private function getSavePath ($ conf) {if (! Isset ($ conf ['static _ base']) {$ save_path = $ conf ['SAVE _ path']; $ save_path. = conf['alias'{.'.html ';} else {if ($ conf ['static _ base'] = 'User _ id') {$ id = (int) $ _ SESSION ['logined _ user'] ['id'];} else {if (IS_GET) {$ id = $ _ GET [$ conf ['static _ base'];} else {$ id = $ _ POST [$ conf ['static _ base'] ;}}$ save_path = $ conf ['SAVE _ path']; $ directory_id = ceil ($ id/$ this-> _ conf ['files _ per_directory ']); $ save_path. = $ Directory_id. '/'; if ($ conf ['Alias']) {$ fileName = $ conf ['Alias']. '-';} $ fileName. = shortid.'.html '; $ save_path. = $ fileName;} return $ save_path;}/* determine whether to generate a static file */private function needStatic () {$ url = explode ('/', /index. php/Article/detail); $ this-> _ controller = $ url [4]; $ this-> _ action = $ url [5]; if (isset ($ this-> _ staticList [$ this-> _ controller]) & isset ($ this-> _ staticList [$ this-> _ controll Er] [$ this-> _ action]) {$ this-> _ needStatic = true ;} if (isset ($ this-> _ deleteList [$ this-> _ controller]) & isset ($ this-> _ deleteList [$ this-> _ controller] [$ this-> _ action]) {$ this-> _ needDeleteStatic = true ;}} /* generate or delete the static file */public function _ static () {if ($ this-> _ needStatic &&! $ This-> _ hasStaticed) {$ html = ob_get_contents (); $ this-> _ mkdir (dirname ($ this-> _ save_path )); file_put_contents ($ this-> _ save_path, $ html);} if ($ this-> _ needDeleteStatic & $ this-> _ hasStaticed) {unlink ($ this-> _ save_path);/* if ($ this-> _ staticAgain) {header ("location: http://www.baidu.com "); // Header (" location: http ://". $ _ SERVER ['http _ host']. '/'. $ _ SERVER ['request _ URI ']);} */}/* Create directory */private function _ mkdir ($ path) {if (! File_exists ($ path) {$ this-> _ mkdir (dirname ($ path); mkdir ($ path, 0777) ;}}?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.