Home > Backend Development > PHP Tutorial > PHP implements the simplest MVC framework example tutorial, mvc framework example tutorial_PHP tutorial

PHP implements the simplest MVC framework example tutorial, mvc framework example tutorial_PHP tutorial

WBOY
Release: 2016-07-13 10:19:16
Original
1027 people have browsed it

PHP implements the simplest MVC framework example tutorial, mvc framework example tutorial

This article describes the process of implementing the MVC framework in PHP in the form of an example, which is relatively easy to understand. Now share it with everyone for your reference. The specific analysis is as follows:

First of all, before learning a framework, we basically need to know what MVC is, that is, model-view-control. To put it bluntly, it is the separation of data control and page implementation. This is how MVC came into being. MVC is divided into There are three levels, and each of the three levels performs its own duties without interfering with each other. First, let’s briefly introduce each level: view is the view, which is the web page, control is the tool for the controller to issue instructions to the system, and model is simply put. It is to retrieve data from the database for processing.

The workflow of MVC is as follows:

1. Viewer-> Call the controller and issue instructions

2. Controller->Follow the command to select a suitable model

3. Model->Select the corresponding data according to the controller instructions

4. Controller->Select the corresponding view according to the command

5. View-> Display the data obtained in the third step as the user wants

A simple example development is as follows. First, develop the first controller. Our naming convention is as follows testController.class.php

<&#63;php
class testController{
function show(){
 
}
}
&#63;>
Copy after login

Secondly write a simple model as follows testModel.class.php

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

Copy after login

The first view file testView.class.php is created to present the data that exists

<&#63;php
class testVies{
  function display($data){
     echo $data;
 
  }
 }
&#63;>
Copy after login

What we have to do next is to test the program according to the five steps mentioned before: The code is as follows. Creation of the test file test.php

<&#63;php
require_once('testController.class.php');
require_once('testModel.class.php');
require_once('testView.class.php');
$testController = new testController();//调用控制器
$testController->show();
&#63;>
Copy after login

<&#63;php
class testController{
  function show(){
      $testModel = new testModel();//选取合适的模型
      $data = $testModel->get();//获取相应的数据
      $testView = new testView();//选择相应的视图
      $testView->display($data);//展示给用户
  }
}
&#63;>
Copy after login

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

Note: The examples in this article are only framework structures, readers can add specific functions by themselves. I hope that the examples described in this article will be helpful to everyone in learning the PHP programming framework.

The simplest example of php mvc

Framework is the embodiment of design pattern
Design pattern is the way of thinking to solve problems

What you should do now is to look back and think about all the things you have encountered in this year’s PHP study. What problems have been solved, which problems should be solved at the technical level, and which problems require you to reconsider your own programming ideas to solve, and then it would be better to consider the framework.
Smarty is just a template, but after you understand the framework, you don’t have to use cakephp or something. You can directly use smarty as part of your own framework. You can even write one yourself without even needing smarty. It would also be nice to use a more suitable template library.
--------------------------
The key issue is that you need to understand why you need a framework, and don’t look at what others say about frameworks. Well, you don’t know what’s good about the framework, and you can’t learn the framework well.
So, you should review the past now, and then you can learn something new.

PHP’s MVC framework, I want a super simple and fool-like hello world demonstration

Framework is the embodiment of design pattern
Design pattern is the way of thinking to solve problems

What you should do now is to look back and think about all the things you have encountered in this year’s PHP study. What problems have been solved, which problems should be solved at the technical level, and which problems require you to reconsider your own programming ideas to solve, and then it would be better to consider the framework.
Smarty is just a template, but after you understand the framework, you don’t have to use cakephp or something. You can directly use smarty as part of your own framework. You can even write one yourself without even needing smarty. It would also be nice to use a more suitable template library.
--------------------------
The key issue is that you need to understand why you need a framework, and don’t look at what others say about frameworks. Well, you don’t know what’s good about the framework, and you can’t learn the framework well.
So, you should review the past now, and then you can learn something new.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/875873.htmlTechArticlephp implements the simplest MVC framework example tutorial, mvc framework example tutorial This article tells the PHP implementation in the form of an example The process of the MVC framework is relatively easy to understand. Now share it with everyone for everyone...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template