php Implementation of the Simplest MVC Framework Example Tutorial

  • 2021-07-18 07:19:47
  • OfStack

This paper describes the process of implementing MVC framework by PHP in the form of an example, which is easy to understand. Now I would like to share it with you for your reference. The specific analysis is as follows:

First, before learning a framework, Basically, we all need to know what mvc is. That is, model-view-control, to put it bluntly, it is the realization of data control and page separation. mvc came into being, mvc is divided into three levels, and the three levels perform their duties without interfering with each other. First of all, each level is briefly introduced: view is the view, that is, web page, control is the tool for the controller to issue instructions to the system, and model is simply to take out data from the database for processing.

The workflow of MVC is as follows:

1. Visitors- > Invoke the controller and issue instructions on it

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

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

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

5. Views- > Display the data obtained in Step 3 as the user wants

Simple example development is as follows. First, develop the first controller. We name it as follows: testController. class. php


<?php
class testController{
function show(){
 
}
}
?>

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


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

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


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

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


<?php
require_once('testController.class.php');
require_once('testModel.class.php');
require_once('testView.class.php');
$testController = new testController();// Invoke 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 to users 
  }
}
?>

Then our browser opens test. php will appear as hello world, indicating that we have succeeded.

Note: The example in this paper is only a framework structure, and readers can add specific functions by themselves. I hope the examples described in this paper are helpful to the study of PHP programming framework.


Related articles: