A php class for page staticization_PHP tutorial
A php class for page staticization
<?php namespace Common; /* * * 功能:页面静态化的创建和删除 * 创建:当且仅当,一个页面需要被静态化并且还未静态化时。 * 删除:当且仅当,一个页面存在静态化页面并且需要被重新静态化时。 * * 作者:郭军周 * * 注 :本类基于ThinkPHP3.2,或者其他具有“单一入口且MVC模式”的其他php框架。 * * 使用方式:在Controller的构造方法中获取其对象;在Controller的销毁方法里,用其对象的_static方法。 * 例:XXXController extends BaseController. * BaseController: * function __construct(){ * $this->__sh = StaticHtml::getInstance(); * } * function __destruct(){ * $this->__sh->_static(); * } * * */ class StaticHtml{ private static $_instance = null; /* 单例模式,自身的引用 */ private $_needStatic = false; /* 是否需要将其静态化 */ private $_needDeleteStatic = false; /* 是否需要删除其静态化的页面 */ private $_hasStaticed = true; /* 是否存在其的静态化页面 */ private $_controller = null; /* 当前被访问的controller */ private $_action = null; /* 当前被访问的action */ // private $_staticAgain = false; /* 删除静态文件后,是否马上重新更新【【注意】再次请求】 */ private $_save_path = null; /* 将要创建或者删除的静态文件的路径 */ private $_conf = array( /* 此文件定义一些静态文件的存放方式 */ 'files_per_directory' => 100 /* 此值不允许被修改,除非是要删除所有已经存在的静态文件,重新缓存 */ ); private $_staticList = array( /* 此数组,定义了需要创建静态化页面的Controller的action */ // 'Base' => array( /* Base为controller name */ // 'aaa' => array( /* aaa为action name */ // 'save_path' => '/StaticHtml/Base/aaa/', /* save_path为生成的静态文件存放的根路径 */ // 'static_base' => 'id', /* static_base为生成静态文件的“依据”。建议为对应数据库的primary_key */ // 'alias' => 'aaa' /* 静态文件的名字,否则为1.html */ // ) // ) 'Mynotes' => array( 'look_notes' => 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( 'save_path' => '/StaticHtml/Information/', 'static_base' => 'user_id', 'alias' => 'information' ) ), 'Courseinfo' => array( 'course_show' => array( 'save_path' => '/StaticHtml/Information/', 'static_base' => 'user_id', 'alias' => 'course' ) ) ); private $_deleteList = array( /* 此数组,定义了需要删除某些静态化页面的Controller的action */ // 'Base' => array( /* Base为controller name */ // 'bbb' => array( /* bbb为action name */ // 'save_path' => '/StaticHtml/Base/aaa/', /* save_path为要删除的静态文件存放的根路径 */ // 'static_base' => 'id', /* static_base为确定静态文件路径的“依据”。建议为对应数据库的primary_key */ // 'alias' => 'aaa' /* 静态文件的名字,否则为1.html */ // ) // ) 'Mynotes' => array( 'edits_notes' => array( 'save_path' => '/StaticHtml/Mynotes/look_notes/', 'static_base' => 'id', 'alias' => 'note' ) ), 'Information' => array( 'save_user_info' => array( 'save_path' => '/StaticHtml/Information/', 'static_base' => 'user_id', 'alias' => 'information' ) ), 'Courseinfo' => array( 'course_update' => array( 'save_path' => '/StaticHtml/Information/', 'static_base' => 'user_id', 'alias' => 'course' ) ) ); private function __construct(){ $this->needStatic(); /* 确定本次请求是否需要静态化 */ $this->hasStaticed(); /* 确定本次请求是否已经存在静态化页面 */ $this->needDeleteStatic(); /* 确定本次请求是否需要删除某些静态页面 */ } /* 确定需要删除的静态文件的存放路径 */ private function needDeleteStatic(){ if($this->_needDeleteStatic){ $save_path = $this->getSavePath($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; } } /* 获取本类的,唯一的,实例化 */ public static function getInstance(){ if(!(self::$_instance instanceof self)){ self::$_instance = new self(); } return self::$_instance; } /* 判断是否存在其静态化的文件 */ 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; } } /* 获取本次请求要生成或者删除的,静态化文件的路径 */ 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 .= $id.'.html'; $save_path .= $fileName; } return $save_path; } /* 确定本次请求,是否需要生成静态化文件 */ private function needStatic(){ $url = explode('/',__ACTION__); $this->_controller = $url[4]; $this->_action = $url[5]; if(isset($this->_staticList[$this->_controller]) && isset($this->_staticList[$this->_controller][$this->_action])){ $this->_needStatic = true; } if(isset($this->_deleteList[$this->_controller]) && isset($this->_deleteList[$this->_controller][$this->_action])){ $this->_needDeleteStatic = true; } } /* 生成,或者删除,静态化文件 */ 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']); }*/ } } /* 创建目录 */ private function _mkdir($path){ if (!file_exists($path)){ $this->_mkdir(dirname($path)); mkdir($path, 0777); } } } ?>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Want to copy a page in Microsoft Word and keep the formatting intact? This is a smart idea because duplicating pages in Word can be a useful time-saving technique when you want to create multiple copies of a specific document layout or format. This guide will walk you through the step-by-step process of copying pages in Word, whether you are creating a template or copying a specific page in a document. These simple instructions are designed to help you easily recreate your page without having to start from scratch. Why copy pages in Microsoft Word? There are several reasons why copying pages in Word is very beneficial: When you have a document with a specific layout or format that you want to copy. Unlike recreating the entire page from scratch

"Methods to handle Laravel pages that cannot display CSS correctly, need specific code examples" When using the Laravel framework to develop web applications, sometimes you will encounter the problem that the page cannot display CSS styles correctly, which may cause the page to render abnormal styles. Affect user experience. This article will introduce some methods to deal with the failure of Laravel pages to display CSS correctly, and provide specific code examples to help developers solve this common problem. 1. Check the file path. First check the path of the CSS file.

Title: Implementation method of page jump in 3 seconds: PHP Programming Guide In web development, page jump is a common operation. Generally, we use meta tags in HTML or JavaScript methods to jump to pages. However, in some specific cases, we need to perform page jumps on the server side. This article will introduce how to use PHP programming to implement a function that automatically jumps to a specified page within 3 seconds, and will also give specific code examples. The basic principle of page jump using PHP. PHP is a kind of

Standby is a new feature in the iOS 17 update that provides a new and enhanced way to access information when your phone is idle quickly. With StandBy, you can conveniently check the time, view upcoming events, browse your calendar, get weather updates for your location, and more. Once activated, the iPhone will intuitively enter standby mode when set to landscape while charging. This feature is perfect for wireless charging points like your bedside table, or when you're away from your iPhone charging during daily tasks. It allows you to swipe through various widgets displayed in standby to access different sets of information from various applications. However, you may want to modify these widgets or even delete some based on your preferences and the information you need frequently. So let's dive into

In iOS, Apple allows you to disable individual home screen pages on your iPhone. It's also possible to rearrange the order of home screen pages and delete pages directly instead of just disabling them. Here's how it works. How to Rearrange Home Screen Pages Touch and hold Space on the Home screen to enter jitter mode. Tap the row of dots that represent Home screen pages. In the Home screen grid that appears, touch and drag a page to rearrange it relative to other pages. Others move in response to your dragging. When you're happy with your new arrangement, tap "Done" in the upper right corner of the screen, then tap "Done" again to exit dither mode. How to Disable or Remove Home Screen Pages Touch and hold Space on the Home screen to enter dither mode. Tap to represent home screen

Page refresh is very common in our daily network use. When we visit a web page, we sometimes encounter some problems, such as the web page not loading or displaying abnormally, etc. At this time, we usually choose to refresh the page to solve the problem, so how to refresh the page quickly? Let’s discuss the shortcut keys for page refresh. The page refresh shortcut key is a method to quickly refresh the current web page through keyboard operations. In different operating systems and browsers, the shortcut keys for page refresh may be different. Below we use the common W

Title: Introduction to how to delete a page of content in Word When editing a document using Microsoft Word, you may sometimes encounter a situation where you need to delete the content of a certain page. You may want to delete a blank page or unnecessary content on a certain page in the document. In response to this situation, we can take some methods to quickly and effectively delete a page of content. Next, some methods to delete a page of content in Microsoft Word will be introduced. Method 1: Delete a page of content First, open the Word document that needs to be edited. Certainly

As the Internet develops, many websites or applications have gradually become more complex. When users use it, they often encounter error pages, the most common of which is the 404 page. The 404 page means that the page being accessed does not exist and is a common error page. For websites or applications, a beautiful 404 page can greatly improve the user experience. In this article, we will introduce how to use ThinkPHP6 to quickly implement a beautiful 404 page. Create a route First, we need to create an err in the route folder
