MVC 是一種用於將應用程式的邏輯層和表現層分離出來的軟體方法。在實踐中,由於這種分離,所以你的頁面中只包含很少的 PHP 指令碼。
模型 代表你的資料結構。通常來說,模型類將包含協助你對資料庫進行增刪改查的方法。
視圖 是要展現給使用者的資訊。一個視圖通常就是一個網頁。
控制器 是模型、視圖以及其他任何處理 HTTP 要求所必須的資源之間的中介,並產生網頁。
自製一個簡易的MVC模型。
1.入口檔案index.php
唯一一個讓瀏覽器直接請求的指令檔
2.控制器controller
協調模型和視圖
3.模型model
提供資料,儲存資料
4.視圖view
負責顯示網頁
5.動作action
控制器中的方法,用於被瀏覽器請求
--------------------檔案夾結構-------------------------
主資料夾mvc_demo
--controllers
--ArticleController.php
--UserController.php
--models
--UserModel.php
--views
--User/index.php
index.php //入口檔案
-----------------------------------------------------------
index.php
$a();
controllers/
UserController.php
UserController{public function index(){//echo "這是User控制器的index方法";//包含檔案並執行個體化一個模型include './models/UserModel.php';//通過模型擷取資料$model = new UserModel();$str=$model->getUser();include './views/User/index.php';}}
models/UserModel.php
views/User/index.php
在瀏覽器中輸入http://localhost:8080/mvc_demo/index.php?c=User&a=index,顯示效果使用者helen。
以上就介紹了一個簡易的MVC模型,包括了UserControl方面的內容,希望對PHP教程有興趣的朋友有所協助。