首先,在學習一個框架之前,基本上我們都需要知道什么是mvc,即model-view-control,說白了就是數(shù)據(jù)控制以及頁面的分離實現(xiàn),mvc就是這樣應運而生的,mvc分為了三個層次,而且三個層次各司其職,互不干擾,首先簡單介紹下,各個層次:view即是視圖,也就是web頁面,control即是控制器 向系統(tǒng)發(fā)出指令的工具,model 簡單說是從數(shù)據(jù)庫中取出數(shù)據(jù)進行處理。
MVC的工作流程如下:
1. 瀏覽者->調(diào)用控制器,對此發(fā)出指令
2. 控制器->按指令選取一個合適的模型
3. 模型->按照控制器指令選取相應的數(shù)據(jù)
4. 控制器->按指令選取相應的視圖
5. 視圖->把第三步取到的數(shù)據(jù)按用戶想要的樣子顯示出來
簡單地實例開發(fā)如下,首先進行第一個控制器的開發(fā) 我們在此命名規(guī)范如下testController.class.php
1<?php
2class testController{
3function show(){
4
5}
6}
7?>
其次書寫一個簡單地模型如下testModel.class.php
1<?php
2
3class testModel{
4function get(){
5return "hello world";
6
7}
8}
9?>
第一個視圖文件的創(chuàng)建testView.class.php 是為了呈現(xiàn)數(shù)據(jù)所存在的
1<?php
2class testVies{
3 function display($data){
4 echo $data;
5
6 }
7 }
8?>
下面我們要做的就是按照之前所說的五步進行程序的測試:代碼如下 測試文件的建立test.php
1<?php
2require_once('testController.class.php');
3require_once('testModel.class.php');
4require_once('testView.class.php');
5$testController = new testController();//調(diào)用控制器
6$testController->show();
7?>
01<?php
02class testController{
03 function show(){
04 $testModel = new testModel();//選取合適的模型
05 $data = $testModel->get();//獲取相應的數(shù)據(jù)
06 $testView = new testView();//選擇相應的視圖
07 $testView->display($data);//展示給用戶
08 }
09}
10?>
而后我們?yōu)g覽器打開test.php 會顯示為hello world,說明我們已經(jīng)成功了。
注:本文實例僅為框架結(jié)構(gòu),具體的功能讀者可以自行添加。希望本文所述實例對大家PHP程序設計框架的學習有所幫助。
更多信息請查看IT技術(shù)專欄