php簡單實現MVC_php執行個體

來源:互聯網
上載者:User

在PHP中使用MVC越來越流行了,特別是在一些開源的架構當中。MVC足以應對大多數的情況,但還有一些情況是其不太適合的,如比較簡單的個人部落格,對於只有幾百篇文章量級的部落格,使用MVC讓人覺得有些太複雜了;同樣對於新浪等門戶網站,使用MVC,將有大量的檔案被載入,對於速度的影響是無法接受的。楓竹夢介紹MVC的基本原理及一種簡單的實現。如下介紹內容適用PHP開發。

PHP中的MVC

MVC[1]在軟體工程中是一種軟體的架構。從php的角度來講MVC有一些不同。

Model(模型),程式應用功能的實現,程式的邏輯的實現。在PHP中負責資料管理,資料產生。

View(視圖),圖形介面邏輯。在PHP中負責輸出,處理如何調用模板、需要的資源檔。

Controller(控制器),負責轉寄請求,對請求處理。在PHP中根據請求決定調用的視圖及使用的資料。

為什麼使用MVC

MVC的主要作用是為了將代碼分層、分類。

MVC的主要目的是為瞭解決Web開發中分離開發與設計工作,使其工作相對獨立。

在這樣的過程中還發現了其他的一些優點,網站的目錄結構更加清晰,網站更易維護與擴充,可以實現模組的複用。

MVC實現

請求URL

首先,約定請求頁面時的URL,以如下結構進行實現:

複製代碼 代碼如下:

localhost/index.php?c=demo&a=index&param=welcome

如果想得到更加優美的URL結構,可以進行最佳化,為由這URL結構最佳化與本文關係不大,以後進行分享。

從上面的參數可以看出,訪問的檔案是index.php,同時含有3個參數分別為c、a、param。

MVC目錄結構

接著,規劃MVC的目錄結構如下:

複製代碼 代碼如下:

 /*
 ├─www                       # 網站根目錄
 │  ├─controller             # 控制器目錄
 │  │  ├─democontroller.php  # demo控制器
 │  ├─model                  # 模型目錄
 │  │  ├─model.php           # model模型
 │  ├─view                   # 視圖目錄
 │  │  ├─index.php           # index視圖
 │  ├─index.php              # 入口檔案
 */

控制器controller

將如下代碼添加到controller/democontroller.php檔案中。

複製代碼 代碼如下:

 // controller/democontroller.php
 class DemoController
 {
     public function index()
     {
     echo 'hello world';
     }
 }// End of the class DemoController
 // End of file democontroller.php

在這個檔案中僅僅定義了一個DemoController的類,且其只包含一個index方法,用於輸出hello world。

將下面代碼添加到入口檔案index.php檔案中。

複製代碼 代碼如下:

 //index.php
 require('controller/democontroller.php');
 $controller = new DemoController();
 $controller->index();
 // End of index.php

在瀏覽器中使用上面的約定的URL進行訪問,看到輸出hello world。當然如果你請求的URL不是那樣,而是如下面所示也能得到同樣的輸出。

複製代碼 代碼如下:

localhost/index.php?c=abc

發現URL中的參數還沒有任何作用。

如果使用下面的URL進行訪問,可以預見不會有任何輸出。

複製代碼 代碼如下:

localhost/controller/democontroller.php

可以看到要想訪問這個網站並得到正確的結果,目前只能通過index.php來訪問,這也是為什麼稱它為入口檔案的原因。現在無論參數如何只能訪問同樣一個頁面,那麼如何來決定顯示不同的結果呢?或者調用不同的控制器呢?

改進入口檔案

下面利用URL中的參數來決定使用哪個控制器。

複製代碼 代碼如下:

 //index.php
 // get runtime controller prefix
 $c_str = $_GET['c'];
 // the full name of controller
 $c_name = $c_str.'controller';
 // the path of controller
 $c_path = 'controller/'.$c_name.'.php';
 // get runtime action
 $method = $_GET['a'];
 // load controller file
 require($c_path);
 // instantiate controller
 $controller = new $c_name;
 // run the controller  method
 $controller->$method();
 // End of index.php

同樣在瀏覽器中使用上面的約定的URL進行訪問,看到輸出hello world。代碼中的注釋已經說明了每一步的目的。當然可以通過改變URL參數中的c與a值來調用不同的controller及其方法,以輸出不同的結果。

視圖View

前面只是使用了控制器controller,同時在入口檔案index.php中實現了動態調用不同的控制器。接著加入視圖將顯示分離。

複製代碼 代碼如下:

 // view/index.php
 class Index {
     public function display($output) {
         // ob_start();
         echo $output;
     }
 }
 // End of index.php

視圖目錄中的index.php檔案中定義了Index方法,且只有一個display()函數,負責將傳遞給它的變數進行輸出。
下面修改控制器檔案。

複製代碼 代碼如下:

 // controller/democontroller.php
 class DemoController
 {
     private $data = 'Hello furzoom!';
     public function index()
     {
     //echo 'hello world';
     require('view/index.php');
     $view = new Index();
     $view->display($data);
     }
 }// End of the class DemoController
 // End of file democontroller.php

在控制器中定義了一個data私人變數,index()方法不再直接輸出,而是使用視圖對象處理輸出。此時,按上面的約定的URL進行訪問時,將看到輸出:

Hello furzoom!
可以根據不同的請求調用不同的視圖類,以不同的形式顯示資料。這樣就將增加了視圖的作用,設計人員可以只針對視圖進行頁面的設計。

模型Model

上面貌似已經很cool了,但是顯示什麼樣的內容是在控制器中直接指定的,希望內容也由URL指定,這樣將資料的處理交給模型來處理。

複製代碼 代碼如下:

 // model/model.php
 class Model {
     private $data = array(
                 'title' => 'Hello furzoom',
                 'welcome' => 'Welcome to furzoom.com',
                 );
     public function getData($key) {
         return $this->data[$key];
     }
 }
 // End of model.php

視圖檔案model.php定義了一個Model類,類中定義了一個getData()的方法,用於返回請求的資料。

同時修改入口檔案index.php如下:

複製代碼 代碼如下:

 //index.php
 // get runtime controller prefix
 $c_str = $_GET['c'];
 // the full name of controller
 $c_name = $c_str.'controller';
 // the path of controller
 $c_path = 'controller/'.$c_name.'.php';
 // get runtime action
 $method = $_GET['a'];
 // get runtime parameter
 $param = $_GET['param'];
 // load controller file
 require($c_path);
 // instantiate controller
 $controller = new $c_name;
 // run the controller  method
 $controller->$method($param);
 // End of index.php

增加了一個參數$param,將其作為控制器的方法調用參數。

還需要修改控制器的方法根據不同參數取得不同的資料。

複製代碼 代碼如下:

 // controller/democontroller.php
 class DemoController
 {
     // private $data = 'Hello furzoom!';
     function index($param)
     {
     // echo 'hello world';
         require('view/index.php');
     require('model/model.php');
     $model = new Model();
     $view = new Index();
     $data = $model->getData($param);
     $view->display($data);
     }
 }// End of the class DemoController
 // End of file democontroller.php

包含需要的視圖檔案和模型檔案,然後產生視圖與模型檔案,接著通過模型對象取得資料,再用視圖對象來輸出取得的資料。

此時,在瀏覽器中使用上面的約定的URL進行訪問,將得到輸出如下:

Welcome to furzoom.com
如下圖:

至此PHP的MVC模式已經基本介紹完成了,剩餘的工作就是根據需要進行添加擴充了,很簡單吧!!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.