欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

php實現(xiàn)url路由分發(fā)功能的方法

小編給大家分享一下php實現(xiàn)url路由分發(fā)功能的方法,希望大家閱讀完這篇文章后大所收獲,下面讓我們一起去探討吧!

創(chuàng)新互聯(lián)建站專注于企業(yè)全網(wǎng)營銷推廣、網(wǎng)站重做改版、橋西網(wǎng)站定制設(shè)計、自適應品牌網(wǎng)站建設(shè)、H5響應式網(wǎng)站商城系統(tǒng)網(wǎng)站開發(fā)、集團公司官網(wǎng)建設(shè)、成都外貿(mào)網(wǎng)站制作、高端網(wǎng)站制作、響應式網(wǎng)頁設(shè)計等建站業(yè)務,價格優(yōu)惠性價比高,為橋西等各大城市提供網(wǎng)站開發(fā)制作服務。

php實現(xiàn)url路由分發(fā)功能的方法:首先要在服務器的配置上對【/router/】路徑進行攔截;然后實現(xiàn)路由分發(fā)器,并獲取請求的uri;最后進行模塊的編寫。

php實現(xiàn)url路由分發(fā)功能的方法

php實現(xiàn)url路由分發(fā)功能的方法:

第一步,首先要在服務器的配置上對/router/路徑進行攔截

php實現(xiàn)url路由分發(fā)功能的方法

調(diào)用某個文件夾目錄下的index.php頁面,假定現(xiàn)在所有模塊使用單獨的文件存放于class目錄下,該目錄與router平級,如下圖所示:

php實現(xiàn)url路由分發(fā)功能的方法

第二步,路由分發(fā)器的實現(xiàn)(index.php)

<!Doctype html>
 <html>
 <head>
 <title>路由測試~~</title>
 <meta http-equiv="content-type" content="text/html; charset=utf-8" />
 </head>
<body>
<?php
date_default_timezone_set("Asia/Shanghai");
 define("MODULE_DIR", "../class/");
 $_DocumentPath = $_SERVER['DOCUMENT_ROOT'];
 $_FilePath = __FILE__;
 $_RequestUri = $_SERVER['REQUEST_URI']; 
 $_AppPath = str_replace($_DocumentPath, '', $_FilePath);  //==>\router\index.php
 $_UrlPath = $_RequestUri;  //==>/router/hello/router/a/b/c/d/abc/index.html?id=3&url=http:
 $_AppPathArr = explode(DIRECTORY_SEPARATOR, $_AppPath);
 
 /**
 * http://192.168.0.33/router/hello/router/a/b/c/d/abc/index.html?id=3&url=http:
 * 
 * /hello/router/a/b/c/d/abc/index.html?id=3&url=http:
 */ 
 for ($i = 0; $i < count($_AppPathArr); $i++) {
   $p = $_AppPathArr[$i];
   if ($p) {
    $_UrlPath = preg_replace('/^\/'.$p.'\//', '/', $_UrlPath, 1);
   }
 }
 
 $_UrlPath = preg_replace('/^\//', '', $_UrlPath, 1);
 
 $_AppPathArr = explode("/", $_UrlPath);
 $_AppPathArr_Count = count($_AppPathArr);
 
 $arr_url = array(
  'controller' => 'index',
   'method' => 'index',
   'parms' => array()
 );
 
 $arr_url['controller'] = $_AppPathArr[0];
 $arr_url['method'] = $_AppPathArr[1];
 
 if ($_AppPathArr_Count > 2 and $_AppPathArr_Count % 2 != 0) {
   die('參數(shù)錯誤');
 } else {
   for ($i = 2; $i < $_AppPathArr_Count; $i += 2) {
     $arr_temp_hash = array(strtolower($_AppPathArr[$i])=>$_AppPathArr[$i + 1]);
     $arr_url['parms'] = array_merge($arr_url['parms'], $arr_temp_hash);
   }
 }
 
 $module_name = $arr_url['controller'];
 $module_file = MODULE_DIR.$module_name.'.class.php';
 $method_name = $arr_url['method'];
 
 if (file_exists($module_file)) {
   include $module_file;
   
   $obj_module = new $module_name();
   
   if (!method_exists($obj_module, $method_name)) {
     die("要調(diào)用的方法不存在");
  } else {
     if (is_callable(array($obj_module, $method_name))) {
       $obj_module -> $method_name($module_name, $arr_url['parms']);
       
       $obj_module -> printResult();
     }
   }
   
 } else {
   die("定義的模塊不存在");
 }
  
 ?>
 
</body>
 </html>

獲取請求的uri,然后拿到要加載的模塊名、調(diào)用方法名,對uri參數(shù)進行簡單的判斷..

第三步,模塊的編寫

根據(jù)上述的uri,我們要調(diào)用的是Hello模塊下的router方法,那么可以在class目錄下定義一個名為Hello.class.php的文件(注意linux下是區(qū)分大小寫的)

<?php
class Hello {
 private $_name;
private $_varValue;
   
   function __construct() {
     
   }
   
   function router() {
     $this->_name = func_get_arg(0);
     $this->_varValue = func_get_arg(1);
   }   
   function printResult() {
    echo $this->_name;
    echo "<p>";
    echo var_dump($this->_varValue);
     echo "</p>";
  }
 }
 ?>

同理,我們可以編寫Ha模塊..

這算是實現(xiàn)了很簡單的url路由分發(fā)功能了…

看完了這篇文章,相信你對php實現(xiàn)url路由分發(fā)功能的方法有了一定的了解,想了解更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!

本文標題:php實現(xiàn)url路由分發(fā)功能的方法
網(wǎng)站鏈接:http://chinadenli.net/article20/gjdsco.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供Google關(guān)鍵詞優(yōu)化企業(yè)建站響應式網(wǎng)站網(wǎng)頁設(shè)計公司域名注冊

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站建設(shè)網(wǎng)站維護公司