PHP implementation of the simplest MVC Framework Example Tutorial

Source: Internet
Author: User
Tags learn php learn php programming php programming

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 <?phpclasstestController{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 <?phpclasstestVies{  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 <?phprequire_once(‘testController.class.php‘);require_once(‘testModel.class.php‘);require_once(‘testView.class.php‘);$testController= newtestController();//调用控制器$testController->show();?>
?
12345678910 <?phpclass 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.