ThinkPHP5Öо²Ì¬»¯ÔõôÉèÖÃ
Ò»¡¢Ê²Ã´ÊǾ²Ì¬»¯
¾²Ì¬»¯Êǽ«ÍøÒ³¶¯Ì¬ÌìÉúµÄÄÚÈÝת»»Îª¾²Ì¬µÄ HTML Îļþ£¬Ê¹µÃÓû§»á¼ûʱֱ½Ó»á¼ûµ½¾²Ì¬Ò³Ã棬Ìá¸ßÍøÕ¾ÐÔÄܵÄÒ»ÖÖÊֶΡ£µ±Óû§»á¼ûÒ»¸ö¶¯Ì¬Ò³Ãæʱ£¬Ð§ÀÍÆ÷»á¾ÓÉһЩ´¦Àí£¬Èç PHP ÆÊÎö¡¢Êý¾Ý¿âÅÌÎʵȵȣ¬²Å»ª¹»½«Ò³Ãæ·µ»Ø¸øÓû§£¬¶ø¾²Ì¬»¯ÔòÊÇʹÓûº´æ»úÖÆ£¬ÔÚÓû§»á¼û¶¯Ì¬Ò³Ãæʱ£¬½«ÌìÉúµÄ¾²Ì¬Îļþ»º´æµ½Ð§ÀÍÆ÷ÉÏ£¬²¢½«Óû§ÇëÇóÖ±½ÓÖض¨Ïòµ½¾²Ì¬ÎļþÉÏ£¬ÒÔµÖ´ï¼õÇáЧÀÍÆ÷¼ç¸ºµÄÄ¿µÄ¡£
¶þ¡¢ThinkPHP 5 Öеľ²Ì¬»¯ÉèÖÃ
ThinkPHP 5 ÖÐĬÈÏÊÇûÓо²Ì¬»¯¹¦Ð§µÄ£¬¿ÉÊÇ¿ÉÒÔͨ¹ýÒÔÏÂÁ½ÖÖ·½·¨¾ÙÐÐÉèÖá£
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
1¡¢Ê¹Óòå¼þ
ThinkPHP 5 ÌṩÁËÒ»¸ö²å¼þ think-html£¬¿ÉÒÔÀû±ãµØʵÏÖÍøÒ³¾²Ì¬»¯£¬Ê¹ÓÃʱֻÐèҪƾ֤ÒÔÏÂÁ½¸ö°ì·¨¼´¿É¡£
£¨1£©ÔÚÓ¦ÓÃĿ¼µÄ config.php ÎļþÖоÙÐÐÉèÖãº
return [ 'template' => [ //¿ªÆôÄ£°å½á¹¹ 'layout_on' => true, //½ç˵½á¹¹Èë¿ÚÎļþ 'layout_name' => 'layout', //¾²Ì¬»¯ºó׺ 'html_suffix' => 'html', // ¿ªÆô¾²Ì¬»º´æ 'tpl_cache' => true, //¿ªÆô¾²Ì¬»º´æ 'cache_html' => true, // Ä£°åĿ¼ 'view_path' => '', ], 'html_cache_rules' => [ '*' => [ '{$_SERVER.REQUEST_URI|md5}', ], ], 'html_cache_time' => 60, 'html_cache_suffix' => '.html', 'html_cache_path' => '../runtime/html', 'html_cache_rule' => ['{$_SERVER.REQUEST_URI|md5}'], ];
µÇ¼ºó¸´ÖÆ
£¨2£©ÔÚÐèÒª¾ÙÐо²Ì¬»¯µÄµØ·½¾ÙÐÐÉèÖãº
//¼ÓÈ뾲̬»º´æ $this->view->engine->layout(true); //¿ªÆô¶¯Ì¬»º´æ $this->view->engine->cache(false);
µÇ¼ºó¸´ÖÆ
2¡¢ÊÖ¶¯ÉèÖÃ
ÊÖ¶¯ÉèÖÃÏà¶ÔÖØ´ó£¬ÐèÒªÊÖ¶¯¿ª·¢ºÍдÈë´úÂ룬¿ÉÊÇ¿ÉÒÔÔ½·¢ÎÞаµØ¿ØÖÆ¡£ÏêϸʵÏÖ°ì·¨ÈçÏ£º
£¨1£©ÔÚÓ¦ÓÃĿ¼ÏÂн¨Ò»¸ö index.html Îļþ£¬´ËÎļþ¼´Îª¾²Ì¬»¯Îļþ¡£
ʹÓà .htaccess ÎļþʵÏÖ·¾¶ÖØд£¬½«¶¯Ì¬Â·¾¶×ª»»Îª¾²Ì¬Â·¾¶¡£ÔÚ .htaccess ÎļþÖпÉÒÔÌí¼ÓÈçÏ´úÂ룺
RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]
µÇ¼ºó¸´ÖÆ
£¨3£©ÔÚ¿ØÖÆÆ÷ÖоÙÐÐÉèÖã¬Ïêϸ´úÂëÈçÏ£º
public function index(){ // ¼ì²é»º´æ $cache = $this->checkHtmlCache(); if(!empty($cache)){ // ¼ÓÔؾ²Ì¬»º´æÎļþ exit($cache); } // »ñÈ¡¶¯Ì¬Êý¾Ý $data = $this->getData(); // ÌìÉú¾²Ì¬Îļþ $this->createHtmlCache($data); // ¼ÓÔؾ²Ì¬»º´æÎļþ exit(file_get_contents($this->html_cache_file)); }
µÇ¼ºó¸´ÖÆ
ÒÔÉϾÍÊÇThinkPHP5Öо²Ì¬»¯ÔõôÉèÖõÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡