php MVC

學習一個框架以前,基本上咱們都須要知道什麼是mvc,即model-view-control,說白了就是數據控制以及頁面的分離實現,mvc就是這樣應運而生的,mvc分爲了三個層次,並且三個層次各司其職,互不干擾,首先簡單介紹下,各個層次,view便是視圖,也就是web頁面,control便是控制器 向系統發出指令的工具,model 簡單說是從數據庫中取出數據進行處理。php

Mvc的工做流程:第一步 瀏覽者->調用控制器,對此發出指令web

                    第二步 控制器->按指令選取一個合適的模型數據庫

                     第三步 模型->按照控制器指令選取相應的數據瀏覽器

                     第四步 控制器->按指令選取相應的視圖mvc

                    第五步 視圖->把第三步取到的數據按用戶想要的樣子顯示出來框架

簡單地實例開發以下,首先進行第一個控制器的開發 咱們在此命名規範以下testController.class.php工具

<?php

 class testController{

function show(){

}

 }

?>

  

         其次書寫一個簡單地模型以下testModel.class.php學習

<?php

class testModel{

function get(){

return "hello world";

}

}

?>

  

         第一個視圖文件的建立testView.class.php 是爲了呈現數據所存在的測試

          

 <?php
class testVies{

    function display($data){

         echo $data;

    }

 }

?>        

  

         下面咱們要作的就是按照以前所說的五步進行程序的測試:代碼以下 測試文件的創建test.phpui

<?php

require_once('testController.class.php');

require_once('testModel.class.php');

require_once('testView.class.php');

$testController = new testController();//調用控制器

$testController->show();

?>

<?php

class testController{

   function show(){

            $testModel = new testModel();//選取合適的模型

            $data = $testModel->get();//獲取相應的數據

            $testView = new testView();//選擇相應的視圖

            $testView->display($data);//展現給用戶

   }

}

?>

  

         然後咱們瀏覽器打開test.php 會顯示爲hello world,說明咱們已經成功了。

相關文章
相關標籤/搜索