Home > Article > Backend Development > Simple implementation of MVC in php, phpmvc_PHP tutorial
Using MVC in PHP is becoming more and more popular, especially in some open source frameworks. MVC is sufficient for most situations, but there are some situations where it is not suitable, such as relatively simple personal blogs. For blogs with only a few hundred articles, using MVC feels a bit too complicated; similarly for blogs with only a few hundred articles, MVC is not suitable for most situations. For portal websites such as Sina, using MVC, a large number of files will be loaded, and the impact on speed is unacceptable. Maple Bamboo Dream introduces the basic principles of MVC and a simple implementation. The following introduction is suitable for PHP development.
MVC in PHP
MVC[1] is a software architecture in software engineering. From a PHP perspective, MVC is a little different.
Model (model), the realization of program application functions and the realization of program logic. Responsible for data management and data generation in PHP.
View, graphical interface logic. Responsible for output in PHP, handling how to call templates and required resource files.
Controller (controller) is responsible for forwarding requests and processing requests. In PHP, the view to be called and the data to be used are determined based on the request.
Why use MVC
The main function of MVC is to layer and classify code.
The main purpose of MVC is to solve the problem of separating development and design work in Web development, making the work relatively independent.
During this process, we also discovered some other advantages. The directory structure of the website is clearer, the website is easier to maintain and expand, and modules can be reused.
MVC implementation
Request URL
First, agree on the URL when requesting the page, and implement it with the following structure:
If you want to get a more beautiful URL structure, you can optimize it. Since this URL structure optimization has little to do with this article, I will share it later.
As can be seen from the above parameters, the file accessed is index.php, which also contains three parameters: c, a, and param.
MVC Directory Structure
Next, plan the directory structure of MVC as follows:
Add the following code to the controller/democontroller.php file.
Copy code
Copy code
Copy code
It was found that the parameters in the URL have no effect yet.
If you use the URL below to access it, you can expect that there will be no output.
You can see that if you want to access this website and get the correct results, you can currently only access it through index.php, which is why it is called the entry file. Now you can only access the same page regardless of the parameters, so how do you decide to display different results? Or what about calling a different controller?
Improve entry file
The following uses the parameters in the URL to decide which controller to use.
Similarly use the above agreed URL to access in the browser and see the output hello world. Comments in the code explain the purpose of each step. Of course, you can call different controllers and their methods by changing the c and a values in the URL parameters to output different results.
View View
We just used the controller controller before, and dynamically called different controllers in the entry file index.php. Then joining the view will show the separation.
The Index method is defined in the index.php file in the view directory, and there is only one display() function, which is responsible for outputting the variables passed to it.
Modify the controller file below.
A data private variable is defined in the controller. The index() method no longer outputs directly, but uses the view object to process the output. At this point, when accessing according to the agreed URL above, you will see the output:
Hello furzoom!
Different view classes can be called according to different requests to display data in different forms. This will increase the role of the view, and designers can design the page only for the view.
Model
The above seems to be very cool, but what kind of content is displayed is directly specified in the controller. It is hoped that the content is also specified by the URL, so that the data processing is handed over to the model.
The view file model.php defines a Model class, which defines a getData() method to return the requested data.
Also modify the entry file index.php as follows:
Added a parameter $param and used it as a method call parameter of the controller.
You also need to modify the controller method to obtain different data based on different parameters.
Contains the required view files and model files, then generates the view and model files, then obtains the data through the model object, and then uses the view object to output the obtained data.
At this time, use the above agreed URL to access in the browser, and you will get the following output:
Welcome to furzoom.com
As shown below:
At this point, the MVC model of PHP has been basically introduced. The remaining work is to add and expand it as needed. It is very simple! !