學(xué)習(xí)一個(gè)框架之前,基本上我們都需要知道什么是mvc,即model-view-control,說(shuō)白了就是數(shù)據(jù)控制以及頁(yè)面的分離實(shí)現(xiàn),mvc就是這樣應(yīng)運(yùn)而生的,mvc分為了三個(gè)層次,而且三個(gè)層次各司其職,互不干擾,首先簡(jiǎn)單介紹下,各個(gè)層次,view即是視圖,也就是web頁(yè)面,control即是控制器 向系統(tǒng)發(fā)出指令的工具,model 簡(jiǎn)單說(shuō)是從數(shù)據(jù)庫(kù)中取出數(shù)據(jù)進(jìn)行處理。
Mvc的工作流程:第一步 瀏覽者->調(diào)用控制器,對(duì)此發(fā)出指令
第二步 控制器->按指令選取一個(gè)合適的模型
第三步 模型->按照控制器指令選取相應(yīng)的數(shù)據(jù)
第四步 控制器->按指令選取相應(yīng)的視圖
第五步 視圖->把第三步取到的數(shù)據(jù)按用戶(hù)想要的樣子顯示出來(lái)
簡(jiǎn)單地實(shí)例開(kāi)發(fā)如下,首先進(jìn)行第一個(gè)控制器的開(kāi)發(fā) 我們?cè)诖嗣?guī)范如下testController.class.php
<?php class testController{ function show(){ } } ?> 其次書(shū)寫(xiě)一個(gè)簡(jiǎn)單地模型如下testModel.class.php
<?php class testModel{ function get(){ return "hello world"; } } ?>
第一個(gè)視圖文件的創(chuàng)建testView.class.php 是為了呈現(xiàn)數(shù)據(jù)所存在的
<?phpclass testVies{ function display($data){ echo $data; } } ?>
下面我們要做的就是按照之前所說(shuō)的五步進(jìn)行程序的測(cè)試:代碼如下 測(cè)試文件的建立test.php
<?php require_once('testController.class.php'); require_once('testModel.class.php'); require_once('testView.class.php'); $testController = new testController();//調(diào)用控制器 $testController->show(); ?>
<?php class testController{ function show(){ $testModel = new testModel();//選取合適的模型 $data = $testModel->get();//獲取相應(yīng)的數(shù)據(jù) $testView = new testView();//選擇相應(yīng)的視圖 $testView->display($data);//展示給用戶(hù) } } ?> 而后我們?yōu)g覽器打開(kāi)test.php 會(huì)顯示為hello world,說(shuō)明我們已經(jīng)成功了。
新聞熱點(diǎn)
疑難解答