學習如何寫PHP MVC架構(1) -- 路由

來源:互聯網
上載者:User

標籤:style   blog   http   io   color   ar   os   sp   檔案   

說到PHP開發web,自然離不開開發架構,開發架構為我們提供了靈活的開發方式,MVC層分離,業務解耦等。。。

第一篇先來簡單點的,說說MVC架構的路由功能。。。

一般的單入口架構路由都是這樣的結構:

domain/index.php/classname/functionname/var1/var2

這裡的index.php 就被稱為入口檔案。。。對於伺服器而言,你這裡訪問的就只有index.php 後面調用的controller 和裡面的方法,甚至傳值都是在架構內部基於PHP層面實現的。

Talk is cheap, show you the code !!

 

首先,先建立好下面的檔案結構

我們來動手試試,怎麼才能訪問到controllers裡面的檔案。。。

在index.php裡面輸入以下內容

print_r($_SERVER);

然後訪問 以下地址試試。

yourdomain/index.php/class/function/var1

這裡作者我是用本地環境的,我訪問的地址是localhost/MVC/index.php/class/function/var1

我貼出最重要的2個變數

[REQUEST_URI] => /MVC/index.php/class/function/var1

[SCRIPT_NAME] => /MVC/index.php

其實路由最基本的原理就在這裡:

通過這2個變數來提取url地址裡的class 和 function,參數等,然後把class include進來,通過PHP的回呼函數  call_user_func_array 調用對應的function和傳遞相應的參數。

接下來上代碼,讀代碼應該比我寫的易懂。哈哈~~

index.php 的內容如下

 1 <?php   2   3 # 定義application路徑   4 define(‘APPPATH‘, trim(__DIR__,‘/‘));   5   6 # 獲得請求地址   7 $root = $_SERVER[‘SCRIPT_NAME‘];   8 $request = $_SERVER[‘REQUEST_URI‘];   9  10 $URI = array();  11  12 # 獲得index.php 後面的地址  13 $url = trim(str_replace($root, ‘‘, $request), ‘/‘);  14  15 # 如果為空白,則是訪問根地址  16 if (empty($url))  17 {  18     # 預設控制器和預設方法  19     $class = ‘index‘;  20     $func = ‘welcome‘;  21 }  22 else 23 {  24     $URI = explode(‘/‘, $url);  25  26     # 如果function為空白 則預設訪問index  27     if (count($URI) < 2)  28     {  29         $class = $URI[0];  30         $func = ‘index‘;  31     }  32     else 33     {  34         $class = $URI[0];  35         $func = $URI[1];  36     }  37 }  38  39  40 # 把class載入進來  41 include(APPPATH . ‘/‘ . ‘application/controllers/‘ . $class . ‘.php‘);  42  43 #執行個體化  44 $obj = new ucfirst($class);  45  46 call_user_func_array(  47     # 調用內部function  48     array($obj,$func),   49     # 傳遞參數  50     array_slice($URI, 2)  51 );  

在application/controllers 裡面添加下面2個檔案

index.php   用於作為預設控制器

 1 <?php   2   3 class Index   4 {   5   6     function welcome()   7     {   8         echo ‘I am default controller‘;   9     }  10  11 }  12  13  14 ?> 

hello.php

 1 <?php   2 class Hello   3 {   4     public function index()   5     {   6         echo ‘hello world‘;   7     }   8   9     public function name($name)  10     {  11         echo ‘hello ‘ . $name;  12     }  13 }  14  15 ?> 

測試一下看看,能不能訪問了。根據上面的路由結構。我們來試試

這個訪問正常,正確調用了hello這個class內部的name方法,然後把參數barbery傳遞過去了。。。

再試試不輸入function name,看看能不能預設調用index。。

答案也是可以的。。。

最後一個,訪問root地址看看

也正確的映射到了預設控制器上。。。

ok,一個簡單的MVC路由功能就完成了。。。

學習如何寫PHP MVC架構(1) -- 路由

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.