Do you understand MVC in php?

怪我咯
Release: 2023-03-13 20:26:01
Original
4882 people have browsed it

The MVC model was proposed by Trygve Reenskaug at the Xerox Research Center in 1970. The earliest reference routine code was written in smalltalk80. It was originally used to solve the interaction problem of user graphical interface in application software.

If you have used the MVC pattern, you will know how useful it is, especially for graphical user interface applications. In addition, MVC is also very useful for web applications. Although web services communicate and the communication process is sometimes interrupted, this also brings some unique challenges and opportunities.

If you come to this chapter looking for a true MVC approach to web applications, I hope you won't be too disappointed with the answers here. The perfect solution does not exist, but there are many examples and related patterns to help you implement MVC effectively. Hopefully, the ideas here can serve as a springboard for your coding efforts and lead you to more research.

Module ViewController

Unlike other design patterns, the MVC pattern is not a class that focuses on describing a class that can be directly designed and coded. Structural patterns. Rather, it is more than just a conceptual guide or example.

Conceptually, the MVC pattern is defined as the bridge between model, view and controller. Both the controller and the view depend on the model, because both the view and the controller have to send data requests to the model. Data is input into your system through the controller, and the results are finally displayed through the view. More specifically for you, a PHP developer, the controller handles every external http request, and the view will generate the http response.

The full name of MVC is Model View Controller, which is the abbreviation of model-view(view)-controller(controller), a software design model , organize the code in a way that separates business logic, data, and interface display, and gathers business logic into one component. While improving and personalizing the interface and user interaction, there is no need to rewrite the business logic.

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. Simply put, model It is to retrieve data from the database for processing.

Mvc workflow: The first step is the viewer->Call the controller and issue an instruction

The second step is the controller->Select a suitable model according to the instruction

Step 3 Model-& GT; Select the corresponding data according to the controller instruction

# Step 4 controller-& gt; select the corresponding view

Step 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

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

Followed by writing a simple model as follows testmodel.class.php

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

The creation of testView.class.php of the first view file is to present the data of

<?php
class testVies{
 
  function display($data){
 
     echo $data;
 
  }
 
 }
 
?>
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 to create the test file test.php

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

When the browser opens test.php, it will be displayed as hello world, indicating that we Already successful.

The above is the detailed content of Do you understand MVC in php?. For more information, please follow other related articles on the PHP Chinese website!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!