The implementation of thinkphp page static function (III.) ____php

Source: Internet
Author: User
Tags create directory

Often said that the page static is divided into two kinds, one is pseudo static, that is, url rewrite, one is true static.

The first two are about two kinds of static methods, which are basically static mechanisms using TP. However, TP write Site page routing is relatively cumbersome and complex, not conducive to engine optimization.

Some time ago to do a website, because the site level is too deep, in the SEO optimization, not included, but only to write their own way to static page, using the method is: in the background of each column and content manual click to static . Background add columns and articles after manually click to generate static page files.

The principle is: each column or content is the only URL path, based on the link to extract the content, and then save the file is static after the file. The page has a tag with a link, all through regular expression matching, replacement, to ensure that in a static page click on the link to jump to the next page is also the html/directory of static page.

First of all write their own way out, the parameters are (column or content URL path, static file save path, generate static file filename):

function createhtml ($url, $dir, $filename) {$content = file_get_contents ($url);   Get content if (!is_dir ($dir)) {//If directory does not exist mkdir (Iconv ("UTF-8", "GBK", $dir), 0777,true); Create directory (777 permissions)}//-------------------------------------------process navigation links-------------------------$listurls = "/[\ ' |\"]\ /app\/index\/articallist\/id\/(. *?)
	[\'|\"]/";
	Preg_match_all ($listurls, $content, $matchlist);
		foreach ($matchlist [1] as $key => $value) {$value = Trim ($value);
		$map [' catdir '] = $value;
		$cat = M (' cms_cate ')->where ($map)->limit (1)->find (); if ($cat [' lv '] = = 1) {$listurl = "\/html/". $value. ".
		Html\ "";
			}else if!empty ($cat [' pid ']) && ($cat [' lv '] = = 2)} {$pid = $cat [' pid '];
			$tmap [' id ']= $pid;
			$pcat = M (' cms_cate ')->where ($tmap)->limit (1)->find ();
			$pcatdir = $pcat [' Catdir ']; $listurl = "\/html/". $pcatdir. " /". $value.".
		Html\ ""; $content = Str_replace ($matchlist [0][$key], $listurl, $content); 
	}
	//-----------------------------------------------------------------------------------
	
	
	//----------------------- -------------process the article List page---------------------------------//List page link to the content page $infourl = "/href=[\ ' |\"]\/app\/index\/ articalinfo\/id\/(. *?)
	[\'|\"]/";
	Preg_match_all ($infourl, $content, $matchlist);
		foreach ($matchlist [1] as $key => $value) {$value = Trim ($value);
		$map [' id '] = $value;
		$cateid = M (' artical ')->where ($map)->limit (1)->getfield (' Cateid ');
		$cat = M (' cms_cate ')->where (' id= '. $cateid)->limit (1)->find (); if ($cat [' lv '] = = 1) {$info = "href= '/html/". $cat [' Catdir ']. " /". $value.".
		HTML ' ";
			}else if!empty ($cat [' pid ']) && ($cat [' lv '] = = 2)} {$pid = $cat [' pid '];
			$tmap [' id ']= $pid;
			$pcat = M (' cms_cate ')->where ($tmap)->limit (1)->find ();      
			$pcatdir = $pcat [' Catdir ']; $info = "href= '/html/". $pcatdir. " /". $cat [' Catdir ']." /". $value.".
		HTML ' "; $content = Str_replace ($matchlist [0][$key], $info, $content); 
	///Article List paging $page = "/<a (. *?) Href=[\ ' |\ ']\/index\/articallist\/id\/(. *?) \/p\/(. *?)
	[\ ' |\ ']>/';
	Preg_match_all ($page, $content, $matchpage); foreach ($matchpage [3] as $key 1 => $value 1) {foreach ($matchpage [1] as $key 2 => $value 2) {if ($key 1 = = $ke
				y2) {$value 1 = trim ($value 1);
				$value 2 = Trim ($value 2); $url = ' <a '. $value 2. ' href= '/html/'. $matchpage [2][0]. ' _ '. $value 1. '.
				HTML ">";
			$content = Str_replace ($matchpage [0][$key 1], $url, $content);  }}//-----------------------------------------------------------------------------------//generated file path and filename $path = $dir. " /". $filename.".
	HTML ";                   if (file_exists ($path)) {unlink ($path); Delete an existing file with the same name}//' W ' write open, point the file pointer to the file header and truncate the file size to zero.  
	If the file does not exist, try creating it.             
	$fp = fopen ($path, "w");       $flag = fwrite ($fp, $content);
	
	Write content fclose ($FP);                        return $flag; Return write identity [success/Failure]}

Different site hierarchy and routing rules are not the same, so the regular matching rules are not the same, we can according to their own web site routing rules for flexible adjustment changes.
Before executing, of course, the following configuration is done in the program entry file index.php in the root directory:

Define (' Html_path ', './html/');//Create a file location for a static page

(1) The homepage of the website is static

Home page static public
function setindexhtml () {
	
	$host = I ("server"). Http_host ");
	
	$url = ' http://'. $host. ' /index.php ';
	
	$dir = './';               Store path
	
	$filename = ' index ';       Classification name when filename
	$res = createhtml ($url, $dir, $filename);
	if ($res) {
		$this->ajaxreturn (Array ("status" => "1", "MSG" => "Operation succeeded"));
	} else{
		$this->ajaxreturn (Array ("status" => "0", "MSG" => "operation Failed"))}
The index.html file generated after the home page is static is saved in the root directory of the Web site, the server-side Setup program entry file is index.html, as shown below:


At this time, access to the domain name, is actually access to the root directory under the index.html static files. And the homepage inside a label jump to other pages of the link are also replaced, click Jump will be routed to the html/directory, looking for the corresponding file name to jump.

(2) The list page of the article is static

List page static public function sethtml () {$_g = F (' F_data ');
	$id = I ("Get.id");
	$m = m (' cms_cate ');
		if (! $id) {$info [' status '] = 0;
		$info [' msg '] = ' id cannot be empty! ';
	$this->ajaxreturn ($info);
	$catdir = Trim ($id);
	$map [' Catdir ']= $catdir;
	$cat = $m->where ($map)->limit (1)->find ();
		if (empty ($cat)) {$info [' status '] = 0;
		$info [' msg '] = ' operation failed, please check this classification! ';
	$this->ajaxreturn ($info);          $psize = $_g[' pagesize '] [' svalue '];
	Each page shows several data $tmap [' cateid '] = $cat [' id '];
	$tmap [' status '] = 1; if ($cat [' model '] = = ' artical ') {$count = M (' artical ')->where ($tmap)->count ();//Total number of articles under current classification}else if ($cat [' model ']
	= = ' Product ') {$count = M (' product ')->where ($tmap)->count ();   } $pagecount = Ceil (Intval ($count)/intval ($psize)); Page number (rounded up) $host = I ("server").
	Http_host "); Multiple pages, list paging if ($pagecount > 1) {for ($i =1; $i <= $pagecount; $i + +) {$url = ' http://'. $host. ' /app/index/articallist/id/'. $catdir. '
			/p/'. $i. '; if ($cat [' lv '] = = 1) {$dir = './html/';
				Store Path}else if ($cat [' pid ']) && ($cat [' lv '] = = 2)} {//The current class has a parent category $pid = $cat [' pid '];
				$tmap [' id ']= $pid;
				$pcat = $m->where ($tmap)->limit (1)->find ();
				$pcatdir = $pcat [' Catdir '];    $dir = './html/'. $pcatdir; Store path} $filename = $catdir. '           _ '. $i; Category name [current filename] $res = createhtml ($url, $dir, $filename);//Create a static file if (! $res) {$this->ajaxreturn ("status" =>
			"0", "MSG" => "operation failed"); exit (); A single page $url = ' http://'. $host. '
	/app/index/articallist/id/'. $catdir. ';                if ($cat [' lv '] = = 1) {$dir = './html/';
		Store Path}else if ($cat [' pid ']) && ($cat [' lv '] = = 2)} {$pid = $cat [' pid '];
		$tmap [' id ']= $pid;
		$pcat = $m->where ($tmap)->limit (1)->find ();
		$pcatdir = $pcat [' Catdir '];      $dir = './html/'. $pcatdir;                    Store path} $filename = $catdir;
	Classification name when filename $res = createhtml ($url, $dir, $filename); if ($res) {$this->ajaxreturn (array ("status" => "1", "MSG" =>"
	Static page Creation Success "));
	}else{$this->ajaxreturn (Array ("status" => "0", "MSG" => "operation failed")); }
}
The difficulty of the list page static is the paging, based on the different page links in the different p parameters of the static, in the judge currently has several pagination, if only one page to generate a file, if there are multiple on the loop to generate multiple files. The following figure:


(3) The content page of the article is static

Content page static public function sethtml () {$id = I ("Get.id");
		if (! $id) {$info [' status '] = 0;
		$info [' msg '] = ' id cannot be empty! ';
	$this->ajaxreturn ($info);
	$map [' id '] = $id;
	$cateid = M (' artical ')->where ($map)->limit (1)->getfield (' Cateid ');
	$cat = M (' cms_cate ')->where (' id= '. $cateid)->limit (1)->find ();
		if (empty ($cat)) {$info [' status '] = 0;
		$info [' msg '] = ' operation failed, please check this classification! ';
	$this->ajaxreturn ($info);     if ($cat [' lv '] = = 1) {$dir = './html/'. $cat [' Catdir '];
		Store Path}else if ($cat [' pid ']) && ($cat [' lv '] = = 2)} {$pid = $cat [' pid '];
		$tmap [' id ']= $pid;
		$pcat = M (' cms_cate ')->where ($tmap)->limit (1)->find ();
		$pcatdir = $pcat [' Catdir ']; $dir = './html/'. $pcatdir. ' /'. $cat [' Catdir ']; Store path} $host = I ("server.
	Http_host "); $url = ' http://'. $host. '
	/app/index/articalinfo/id/'. $id. ';        $filename = $id;
	ID when filename $res = createhtml ($url, $dir, $filename); if ($res) {$this->ajaxreturn (array ("status" => "1", "MSG" => "Static page Creation Success "));
	}else{$this->ajaxreturn (Array ("status" => "0", "MSG" => "operation failed"));
 }
}
The background each add an article, all manually static once, will be in the html/directory under the category of the article, and then the article ID for the name of the generation of HTML files. For example, a news article ID 32, the News column, will be in the html/news/directory to generate 32.html.


(4) One-click Static all article content page pages under current classification

One-click Static all Article content Page page public Function Sethtmlall () {C (' default_theme ', ' app/default/') under current classification;
	$data = I (' Get. ');
	$cateid = $data [' Cateid '];
	$map [' cateid '] = $cateid;
	$map [' status '] = 1;
	$artical = M (' artical ')->where ($map)->select ();
	$cat = M (' cms_cate ')->where (' id= '. $cateid)->limit (1)->find ();
		if (empty ($cat)) {$info [' status '] = 0;
		$info [' msg '] = ' operation failed, please check this classification! ';
	$this->ajaxreturn ($info); } $host = I ("server.")
	Http_host "); foreach ($artical as $key => $value) {$url = ' http://'. $host. '
		/app/index/articalinfo/id/'. $value [' id ']. ';                     if ($cat [' lv '] = = 1) {$dir = './html/'. $cat [' Catdir '];
			Store Path}else if ($cat [' pid ']) && ($cat [' lv '] = = 2)} {$pid = $cat [' pid '];
			$tmap [' id ']= $pid;
			$pcat = M (' cms_cate ')->where ($tmap)->limit (1)->find ();
			$pcatdir = $pcat [' Catdir ']; $dir = './html/'. $pcatdir. '        /'. $cat [' Catdir '];
		Store path} $filename = $value [' id '];       $res = createhtml ($url, $dir, $filename);           Create a static file if (! $res) {$this->ajaxreturn (array ("status" => "0", "MSG" => "operation failed"));
}} $this->ajaxreturn (Array ("status" => "1", "MSG" => "static page Creation Success"));
 }
For example, there are many articles under the News column, a manual static too slow, you can achieve a key static. To find news articles in the database first, and then loop through the static, all the news article static pages will be generated in the html/news/directory. The following figure:

At this point again visit the site, access to all the static pages, access to the URL will also become customized, simplifying the level of the site, easy to engine optimization.

Static case:

Before Static: http://www.anumuying.com/index.php

After static: http://www.anumuying.com

Different site hierarchy and routing rules are not the same, so the URL is different, while the regular matching rules are not the same, we can according to their own web site routing rules for flexible adjustment changes.

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.