首頁  >  文章  >  後端開發  >  php url偽靜態化的實作方法詳解

php url偽靜態化的實作方法詳解

WBOY
WBOY原創
2016-07-25 08:56:051143瀏覽
  1. //將url轉換成靜態url

  2. function url_rewrite($file, $params = array () , $html = "", $rewrite = true) {
  3. if ($rewrite) { //開發階段是不要rewrite,所在開發的時候,把$rewrite = false
  4. $url = ( $file == 'index') ? '' : '/' . $file;
  5. if (! empty ( $params ) && is_array ( $params )) {
  6. $url .= '/' . implode ( '/', array_slice($params, 0 , 2));
  7. $param = array_slice($params, 2);
  8. foreach($param as $key => $value){
  9. $ url .= '/' . $key . '/' . urlencode ( $value );
  10. }
  11. }
  12. if (! empty ( $html )) {
  13. $url .= '. ' . $html;
  14. }
  15. } else {
  16. $url = ($file == 'index') ? '/' : '/' . $file;
  17. if ( substr ( $url, - 4 ) != '.php' && $file != 'index') {
  18. $url .= '.php';
  19. }
  20. if (! empty ( $params ) && is_array ( $params )) {
  21. $url .= '?' . http_build_query ( $params );
  22. }
  23. }
  24. return $url;
  25. }
  26. }
  27. return $url; >
  28. echo url_rewrite ( 'test', array ('class' => "User", 'act' => 'check', 'name' => 'tank','page'=>5 ) );echo "
    ";
  29. //$rewrite = false的情況下,顯示如下/test.php?class=User&act=check&name=tank
  30. echo url_rewrite ( 'test. php', array ('class' => "User", 'act' => 'check', 'name' => 'tank' ) );echo "
    ";

  31. //$rewrite = true的情況下,顯示如下/test.php/User/check/tank
  32. echo url_rewrite ( 'test', array ('class' => "User", 'act' => 'check', 'name' => 'tank' ) );echo "
    ";
  33. //$rewrite = true的情況下,顯示如下/test/User/check/tank
  34. echo url_rewrite ( 'test', array ('class' => "User", 'act' => 'check', 'name' => 'tank' ), 'html' );echo "
    ";
  35. //$rewrite = true的情況下,顯示如下/test/User/check/tank.html
  36. ?>
複製程式碼

"User",'act'=>'check','name'=>'tank'));?>">test

以上把動態url轉換成靜態的url,頁面中會產生連結如下: test 此時如果直接點擊的話,一定會報404錯誤的,因為根不可能找到tank這個目錄的。 需要要把找不到的目錄和檔案指定一個php檔案。這個需要用到apache,nginx,或htaccess等。

三,指定一個統一入口
  1. RewriteCond %{REQUEST_FILENAME} !-f //找不到檔案
  2. RewriteCond %{REQUEST_FENAME} !-dILENAME}
RewriteRule . /test3/index.php [L]
複製程式碼

代碼說明: 如果找不到目錄到index.php文件,如果找不到文件,也轉到index.php。 當訪問http://localhost/test3/test.php/User/check/tank時候,就會轉到index.php。 以下內容都是以http://localhost/test3/test.php/User/check/tank這種重寫的方式來操作的。

四,index.php文件

  1. $filename = $_SERVER['REQUEST_URI']; //請求的url
  2. /**請求的url,"/test3/test.php/User/check/tank"
  3. * test.php 要去的php檔案
  4. * User 是class名稱
  5. * check 是class中的方法名稱
  6. * tank 是要傳到check的參數
  7. * by bbs.it-home.org
  8. */
  9. preg_match("/(w+.php)/",$filename,$match); //找php檔案名稱
  10. $array = explode('/', $filename); //將靜態url進行分割
  11. $key = array_keys($array,$match[0]); //得到檔案所對應的下標Array ( [0] => 2 )
  12. $file_array = array_slice($array,0,$key[0]+1); //Array ( [0] => [1] => test3 [2] => test.php )
  13. $ param_array = array_slice($array,$key[0]+1); //Array ( [0] => User [1] => check [2] => tank )
  14. $file_path = implode( '/',$file_array);
  15. if($array[$key[0]] != "index.php"){
  16. include_once($array[$key[0]]); //包函請求url中的php檔,這裡是test.php
  17. }
  18. if(class_exists($param_array[0])){ //判斷一下test.php這個檔案有沒有User這個class
  19. $obj = new $param_array[0];
  20. if(method_exists($obj,$param_array[1])){ //判斷User這個class中有沒有check這個方法
  21. $obj->$param_array[1]($param_array[3]); //呼叫這個方法,結果是(我的名子叫tank)
  22. }
  23. }
  24. ? >
複製程式碼

五,test.php文件

  1. class User {
  2. public function check($name){
  3. echo "我的名子叫". $name;
  4. }
  5. }
  6. ?>
複製程式碼

至此,當造訪http://localhost/test3/test. php/User/check/tank時,就不會報錯了。



陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn