關於靜態化,PHP的靜態化分為:純靜態和偽靜態。其中純靜態又分為:局部純靜態和全部純靜態。這里將的是全部純靜態。
純靜態化實例:
db.php、template.php 、test.php
db.php代碼如下:
1 <?php 2 header("content-type:text/html;charset=utf-8"); 3 /* 4 * 單例模式連接數據庫 5 */ 6 class DB{ 7 static private $_instance; //非public的類的實例的靜態成員變量 8 static private $_connectSource; //連接數據庫返回的資源句柄 9 private $_dbConfig = array( 10 'host'=>'127.0.0.1', 11 'username'=>'root', 12 'pwd'=>'root', 13 'database'=>'mvc' 14 ); 15 16 private function __construct(){ //非public 的構造函數 17 } 18 19 static public function getInstance(){ //訪問實例的公共靜態方法 20 if(!self::$_instance instanceof self){ 21 self::$_instance = new self(); 22 } 23 return self::$_instance; 24 } 25 26 public function connect(){ 27 if(!self::$_connectSource){ 28 //連接mysql服務 29 self::$_connectSource = @mysql_connect($this->_dbConfig['host'],$this->_dbConfig['username'],$this->_dbConfig['pwd']); 30 if(!self::$_connectSource){ 31 //拋出異常 32 throw new Exception('mysql connect error'.mysql_error()); 33 } 34 //選擇數據庫 35 mysql_select_db($this->_dbConfig['database'],self::$_connectSource); 36 //設置字符集 37 mysql_query('set names "UTF8"',self::$_connectSource); 38 } 39 return self::$_connectSource; //返回資源 40 } 41 }
template.php代碼如下:
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>新聞中心</title> 6 7 </head> 8 <body> 9 10 <div class="container"> 11 <h3>新聞列表</h3> 12 <ul class="list-group"> 13 <?php foreach ($news as $key => $value) { ?> 14 <li class="list-group-item"><a href="#"><?php echo $value['topic_title'];?></a></li> 15 <?php } ?> 16 </ul> 17 </div> 18 </body> 19 </html>
test.php代碼如下:
1 <?php 2 //1、連接數據庫,然后從數據庫里面獲取數據 3 //2、把獲取到的數據填充到模板文件里面 4 //3、需要把動態的頁面轉化為靜態頁面,生成純靜態化文件 5 header("content-type:text/html;charset=utf-8"); 6 if(is_file('index.shtml')&&(time()-filemtime('index.shtml'))<300){ 7 require_once('index.shtml'); 8 }else{ 9 require_once('db.php'); 10 $connect = DB::getInstance()->connect(); 11 $sql = 'select * from ask_topic'; 12 $result=mysql_query($sql,$connect); 13 $news=array(); 14 while($new=mysql_fetch_assoc($result)){ 15 $news[]=$new; 16 } 17 //開啟緩存區 18 ob_start(); 19 //引入模板文件 20 require_once('template.php');//動態文件。template.php界面同樣進過緩沖區 21 file_put_contents('index.shtml', ob_get_contents());// 當我們第一次訪問 index.php時,服務器將為我們生成一個靜態文件index.shtml。 22 }
靜態化頁面中如何想加載動態的內容如何處理?
Ajax技術:jquery中ajax請求方式$.ajax()
實現步驟:編寫接口-》ajax請求接口操作
接口數據:1.獲取數據
2. 把我們獲取到的數據組裝成接口數據通信
接口hot.php代碼如下:
1 <?php 2 header("content-type:text/html;charset=utf-8"); 3 require_once 'db.php'; //引用連接數據庫 4 $connect = DB::getInstance()->connect(); 5 $sql = 'select `topic_desc` from ask_topic'; 6 $result=mysql_query($sql,$connect); 7 $hots=array(); 8 while($hot=mysql_fetch_assoc($result)){ 9 $hots[]=$hot; 10 } 11 return show(200,'success',$hots); 12 function show($code=0,$message="error",$data=array()){ 13 $result=array( 14 'code'=>$code, 15 'message'=>$message, 16 'data'=>$data, 17 ); 18 echo json_encode($result); 19 }
再次修改template.php 增加ajax動態調用部分。修改之后的代碼如下:
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>新聞中心</title> 6 <script type="text/javascript" src="http://localhost/2019/jquery.min.js"></script> 7 </head> 8 <body> 9 10 <div class="container"> 11 <h3>新聞列表</h3> 12 <ul class="list-group"> 13 <?php foreach ($news as $key => $value) { ?> 14 <li class="list-group-item"><a href="#"><?php echo $value['topic_title'];?></a></li> 15 <?php } ?> 16 </ul> 17 </div> 18 19 <h3>動態調用部分內容</h3> 20 <ul class="list-group" id="hot"> 21 22 </ul> 23 <script type="text/javascript"> 24 $.ajax({ 25 url:"http://localhost/2019/hot.php", 26 dataType:'json', 27 type:'post', 28 success:function(result){ 29 if(result.code==200){ 30 html=''; 31 $.each(result.data,function(key,value){ 32 33 html+='<li><a href="/">'+value.topic_desc+'</a></li>'; 34 }); 35 $("#hot").html(html); 36 } 37 } 38 }); 39 </script> 40 41 42 </body> 43 </html>
訪問test.php的時候 就可以顯示效果