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
<?php class testController{ function show(){ } } ?>
Secondly write a simple model as follows testModel.class.php
<?php class testModel{ function get(){ return "hello world"; } } ?>
The first view file testView.class.php is created to present the data that exists
<?php class testVies{ function display($data){ echo $data; } } ?>
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
<?php require_once('testController.class.php'); require_once('testModel.class.php'); require_once('testView.class.php'); $testController = new testController();//调用控制器 $testController->show(); ?>
<?php class testController{ function show(){ $testModel = new testModel();//选取合适的模型 $data = $testModel->get();//获取相应的数据 $testView = new testView();//选择相应的视图 $testView->display($data);//展示给用户 } } ?>
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.
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.
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.