PHP to implement the simplest example of an MVC framework tutorial _php Tips

Source: Internet
Author: User

This paper describes the process of implementing the MVC framework with an example, which is easy to understand. We are now sharing it with you 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, the white is the data control and the separation of the implementation of the page, MVC is the emergence of this, MVC is divided into three levels, and three levels of each other, non-interference, First of all, a simple introduction, all levels: view is the views, that is, the Web page, control is the controller to send instructions to the System tool, model simply said that from the database to remove data processing.

The workflow for MVC is as follows:

1. The caller-> the controller, issuing instructions

2. controller-> Select a suitable model according to the instruction

3. model-> Select the corresponding data according to the controller instruction

4. controller-> Select the appropriate view by instruction

5. view-> The data taken from the third step as the user wants it to appear

A simple example is developed as follows, first the development of the first controller we hereby name the specification as follows testController.class.php

<?php
class testcontroller{
function Show () {
 
}
}
?>

Second, write a simple model as follows testModel.class.php

<?php
 
class testmodel{
function Get () {return
' Hello World ';
 
}
? >

The first view file was created testView.class.php to render the data

<?php
class testvies{
  function display ($data) {
     echo $data
 
  }}
? >

What we are going to do is to follow the previous five steps to test the program: the code follows the establishment of the test file test.php

<?php
require_once (' testController.class.php ');
Require_once (' testModel.class.php ');
Require_once (' testView.class.php ');
$testController = new TestController ();//Call Controller
$testController->show ();
? >

<?php
class testcontroller{
  function Show () {
      $testModel = new Testmodel ()//Select the appropriate model
      $DATA = $ Testmodel->get ()//get the corresponding data
      $testView = new TestView ()//Select the appropriate view
      $testView->display ($data);//Show the user
  }
}
? >

Then our browser opens test.php will show Hello World, which means we have succeeded.

Note: This article is only a frame structure, the specific features of the reader can be added. I hope that the example described in this article will help you learn the PHP programming framework.

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.