亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

php簡單實(shí)現(xiàn)MVC

 更新時(shí)間:2015年02月05日 11:33:46   投稿:hebedich  
文章簡單介紹了MVC的概念,php中的MVC,使用MVC的原因,以及如何簡單是先MVC,非常詳細(xì),這里推薦給大家。

在PHP中使用MVC越來越流行了,特別是在一些開源的框架當(dāng)中。MVC足以應(yīng)對(duì)大多數(shù)的情況,但還有一些情況是其不太適合的,如比較簡單的個(gè)人博客,對(duì)于只有幾百篇文章量級(jí)的博客,使用MVC讓人覺得有些太復(fù)雜了;同樣對(duì)于新浪等門戶網(wǎng)站,使用MVC,將有大量的文件被加載,對(duì)于速度的影響是無法接受的。楓竹夢(mèng)介紹MVC的基本原理及一種簡單的實(shí)現(xiàn)。如下介紹內(nèi)容適用PHP開發(fā)。

PHP中的MVC

MVC[1]在軟件工程中是一種軟件的架構(gòu)。從php的角度來講MVC有一些不同。

Model(模型),程序應(yīng)用功能的實(shí)現(xiàn),程序的邏輯的實(shí)現(xiàn)。在PHP中負(fù)責(zé)數(shù)據(jù)管理,數(shù)據(jù)生成。

View(視圖),圖形界面邏輯。在PHP中負(fù)責(zé)輸出,處理如何調(diào)用模板、需要的資源文件。

Controller(控制器),負(fù)責(zé)轉(zhuǎn)發(fā)請(qǐng)求,對(duì)請(qǐng)求處理。在PHP中根據(jù)請(qǐng)求決定調(diào)用的視圖及使用的數(shù)據(jù)。

為什么使用MVC

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

MVC的主要目的是為了解決Web開發(fā)中分離開發(fā)與設(shè)計(jì)工作,使其工作相對(duì)獨(dú)立。

在這樣的過程中還發(fā)現(xiàn)了其他的一些優(yōu)點(diǎn),網(wǎng)站的目錄結(jié)構(gòu)更加清晰,網(wǎng)站更易維護(hù)與擴(kuò)展,可以實(shí)現(xiàn)模塊的復(fù)用。

MVC實(shí)現(xiàn)

請(qǐng)求URL

首先,約定請(qǐng)求頁面時(shí)的URL,以如下結(jié)構(gòu)進(jìn)行實(shí)現(xiàn):

復(fù)制代碼 代碼如下:

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

如果想得到更加優(yōu)美的URL結(jié)構(gòu),可以進(jìn)行優(yōu)化,為由這URL結(jié)構(gòu)優(yōu)化與本文關(guān)系不大,以后進(jìn)行分享。

從上面的參數(shù)可以看出,訪問的文件是index.php,同時(shí)含有3個(gè)參數(shù)分別為c、a、param。

MVC目錄結(jié)構(gòu)

接著,規(guī)劃MVC的目錄結(jié)構(gòu)如下:

復(fù)制代碼 代碼如下:

 /*
 ├─www                       # 網(wǎng)站根目錄
 │  ├─controller             # 控制器目錄
 │  │  ├─democontroller.php  # demo控制器
 │  ├─model                  # 模型目錄
 │  │  ├─model.php           # model模型
 │  ├─view                   # 視圖目錄
 │  │  ├─index.php           # index視圖
 │  ├─index.php              # 入口文件
 */

控制器controller

將如下代碼添加到controller/democontroller.php文件中。

復(fù)制代碼 代碼如下:

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

在這個(gè)文件中僅僅定義了一個(gè)DemoController的類,且其只包含一個(gè)index方法,用于輸出hello world。

將下面代碼添加到入口文件index.php文件中。

復(fù)制代碼 代碼如下:

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

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

復(fù)制代碼 代碼如下:

localhost/index.php?c=abc

發(fā)現(xiàn)URL中的參數(shù)還沒有任何作用。

如果使用下面的URL進(jìn)行訪問,可以預(yù)見不會(huì)有任何輸出。

復(fù)制代碼 代碼如下:

localhost/controller/democontroller.php

可以看到要想訪問這個(gè)網(wǎng)站并得到正確的結(jié)果,目前只能通過index.php來訪問,這也是為什么稱它為入口文件的原因?,F(xiàn)在無論參數(shù)如何只能訪問同樣一個(gè)頁面,那么如何來決定顯示不同的結(jié)果呢?或者調(diào)用不同的控制器呢?

改進(jìn)入口文件

下面利用URL中的參數(shù)來決定使用哪個(gè)控制器。

復(fù)制代碼 代碼如下:

 //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進(jìn)行訪問,看到輸出hello world。代碼中的注釋已經(jīng)說明了每一步的目的。當(dāng)然可以通過改變URL參數(shù)中的c與a值來調(diào)用不同的controller及其方法,以輸出不同的結(jié)果。

視圖View

前面只是使用了控制器controller,同時(shí)在入口文件index.php中實(shí)現(xiàn)了動(dòng)態(tài)調(diào)用不同的控制器。接著加入視圖將顯示分離。

復(fù)制代碼 代碼如下:

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

視圖目錄中的index.php文件中定義了Index方法,且只有一個(gè)display()函數(shù),負(fù)責(zé)將傳遞給它的變量進(jìn)行輸出。
下面修改控制器文件。

復(fù)制代碼 代碼如下:

 // 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

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

Hello furzoom!
可以根據(jù)不同的請(qǐng)求調(diào)用不同的視圖類,以不同的形式顯示數(shù)據(jù)。這樣就將增加了視圖的作用,設(shè)計(jì)人員可以只針對(duì)視圖進(jìn)行頁面的設(shè)計(jì)。

模型Model

上面貌似已經(jīng)很cool了,但是顯示什么樣的內(nèi)容是在控制器中直接指定的,希望內(nèi)容也由URL指定,這樣將數(shù)據(jù)的處理交給模型來處理。

復(fù)制代碼 代碼如下:

 // 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定義了一個(gè)Model類,類中定義了一個(gè)getData()的方法,用于返回請(qǐng)求的數(shù)據(jù)。

同時(shí)修改入口文件index.php如下:

復(fù)制代碼 代碼如下:

 //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

增加了一個(gè)參數(shù)$param,將其作為控制器的方法調(diào)用參數(shù)。

還需要修改控制器的方法根據(jù)不同參數(shù)取得不同的數(shù)據(jù)。

復(fù)制代碼 代碼如下:

 // 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

包含需要的視圖文件和模型文件,然后生成視圖與模型文件,接著通過模型對(duì)象取得數(shù)據(jù),再用視圖對(duì)象來輸出取得的數(shù)據(jù)。

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

Welcome to furzoom.com
如下圖:

至此PHP的MVC模式已經(jīng)基本介紹完成了,剩余的工作就是根據(jù)需要進(jìn)行添加擴(kuò)充了,很簡單吧!!

相關(guān)文章

最新評(píng)論