學習一個框架之前,基本上我們都需要知道什么是mvc,即model-view-control,說白了就是數據控制以及頁面的分離實現,mvc就是這樣應運而生的,mvc分為了三個層次,而且三個層次各司其職,互不干擾,首先簡單介紹下,各個層次,view即是視圖,也就是web頁面,control即是控制器 向系統發出指令的工具,model 簡單說是從數據庫中取出數據進行處理。
Mvc的工作流程:第一步 瀏覽者->調用控制器,對此發出指令
第二步 控制器->按指令選取一個合適的模型
第三步 模型->按照控制器指令選取相應的數據
第四步 控制器->按指令選取相應的視圖
第五步 視圖->把第三步取到的數據按用戶想要的樣子顯示出來
簡單地實例開發如下,首先進行第一個控制器的開發 我們在此命名規范如下testController.class.php
1
2
3
4
5
6
7
8
9
10
11
|
<?php class testController{ function show(){ } } ?> |
其次書寫一個簡單地模型如下testModel.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<?php class testModel{ function get(){ return "hello world" ; } } ?> |
第一個視圖文件的創建testView.class.php 是為了呈現數據所存在的
1
2
3
4
5
6
7
8
9
10
11
12
|
<?php class testVies{ function display( $data ){ echo $data ; } } ?> |
下面我們要做的就是按照之前所說的五步進行程序的測試:代碼如下 測試文件的建立test.php
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<?php require_once ( 'testController.class.php' ); require_once ( 'testModel.class.php' ); require_once ( 'testView.class.php' ); $testController = new testController(); //調用控制器 $testController ->show(); ?> |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<?php class testController{ function show(){ $testModel = new testModel(); //選取合適的模型 $data = $testModel ->get(); //獲取相應的數據 $testView = new testView(); //選擇相應的視圖 $testView ->display( $data ); //展示給用戶 } } ?> |
而后我們瀏覽器打開test.php 會顯示為hello world,說明我們已經成功了。