Php implementation of the simplest MVC framework example tutorial

Source: Internet
Author: User
This article mainly introduces the simplest MVC framework implementation tutorial in php, describes the running principle and simple implementation method of the MVC framework, and has good reference value, you can refer to the following example to describe how PHP implements the MVC framework, which is easy to understand. I would like to share it with you for your reference. The specific analysis is as follows:

First, before learning a framework, we basically need to know what mvc is, that is, model-view-control. to put it simply, data control and page separation are implemented, mvc came into being in this way. mvc is divided into three layers, and each layer performs its own duties and does not interfere with each other. First, let's briefly introduce each layer: view is view, that is, web page, control is a tool used by the controller to send commands to the system. The model simply refers to extracting data from the database for processing.

The MVC workflow is as follows:

1. viewer-> call the controller to issue the command

2. Controller-> select an appropriate model by command

3. model-> select the corresponding data according to the controller command

4. Controller-> select the view according to the command

5. View-> display the data obtained in step 3 as you want.

Simple instance development is as follows. First, we will develop the first controller. the naming rules here are as follows: testController. class. php

<?phpclass testController{function show(){ }}?>

Next, write a simple model: testModel. class. php

<?php class testModel{function get(){return "hello world"; }}?>

TestView. class. php is created for the first view file to present the data

<?phpclass testVies{  function display($data){     echo $data;   } }?>

Next we need to test the program according to the five steps mentioned above: the code is as follows: test. php is created in the test file.

<? Phprequire_once ('testcontroller. class. php '); require_once ('testmodel. class. php '); require_once ('testview. class. php '); $ testController = new testController (); // call the controller $ testController-> show ();?>

<? Phpclass testController {function show () {$ testModel = new testModel (); // select an appropriate model $ data = $ testModel-> get (); // obtain the corresponding data $ testView = new testView (); // select the corresponding view $ testView-> display ($ data); // display it to the user}?>

Then we open test. php in the browser and it will be displayed as hello world, indicating that we have succeeded.

Note: the examples in this article are only framework structures. you can add specific functions on your own. I hope the examples 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.