In this paper, the process of implementing the MVC framework in PHP is described in the form of an example, which is relatively easy to understand. We are now sharing it for your reference. The specific analysis is as follows:
First of all, before learning a framework, basically we all need to know what is MVC, that is, Model-view-control, is the data control and the separation of the page implementation, MVC is born, MVC is divided into three levels, and three levels of their respective, non-interference, First of all, a simple introduction, the various levels: view is views, that is, the Web page, control is the controller to the system issued instructions, the model is simply to remove data from the database for processing.
The MVC workflow is as follows:
1. Caller---Call the controller to issue instructions
2. Select a suitable model according to the instructions of the Controller
3. Select the corresponding data according to the controller instruction
4. Select the appropriate view by command
5. View, take the third step to the data as the user wants to show
A simple example is developed as follows, first to develop the first controller we have in this naming specification as follows testController.class.php
?
1234567 |
<?php class testController{ function show(){ } } ?> |
Second, write a simple model as follows testModel.class.php
?
123456789 |
<?php class testModel{ function get(){ return "hello world" ; } } ?> |
The first view file is created testView.class.php to present the data
?
12345678 |
<?php class testVies{ function display( $data ){ echo $data ; } } ?> |
The next thing we do is to follow the previous five steps to test the program: the code follows the establishment of test files test.php
?
1234567 |
<?php require_once ( ‘testController.class.php‘ ); require_once ( ‘testModel.class.php‘ ); require_once ( ‘testView.class.php‘ ); $testController = new testController(); //调用控制器 $testController ->show(); ?> |
?
12345678910 |
<?php
class testController{
function show(){
$testModel =
new testModel();
//选取合适的模型
$data =
$testModel
->get();
//获取相应的数据
$testView =
new testView();
//选择相应的视图
$testView
->display(
$data
);
//展示给用户
}
}
?>
|
Then our browser opens test.php will be displayed as Hello World, indicating that we have succeeded.
Note: This example is only for frame structure, and the specific function reader can add it yourself. It is hoped that the examples mentioned in this article will help us to learn PHP programming framework.
PHP implementation of the simplest MVC Framework Example Tutorial