You may also be interested in their working principles. Let me talk about what an MVC framework looks like.
Routing mechanism
On the Internet, we all provide services through URLs, so different URLs have different services. Users access different pages and receive different services. So how does our service distinguish different services through URLs.
Our web program must find different files through URLs and perform different business logic processing. Our routing mechanism is to find the corresponding controller and action based on the URL, and then the action performs specific business logic processing.
A simple controller
The specific corresponding rules are mapped differently by different frameworks. The following is the URL routing of the CodeIgniter framework. It will try its best to try various possibilities to analyze the URL situation.
File path/system/core/URI.php
After the above attempt, the next step is how to use the routing mechanism to load the correct controller.
Controller loading mechanism
Let’s take a look at how the Codeigniter framework is loaded into the controller and calls actions.
There is the following code in /system/core/Codeigniter.php. Codeigniter will assign values based on the value in $_SERVER['PATH_INFO] before this (this all depends on its own settings. By default, CI will have many if branches for judgment).
That’s it, our controller and its methods are called through this. The next step is to write your own business logic code.
View view display
After our business logic code is written, we need to display the page. The page calls of many common MVC frameworks are written like this.
In these few lines of code, they are all the essence. These are very important, all of them are built-in functions of PHP. Next, let’s analyze them in detail.
Look at the first extract($var). This function imports variables from an array into the current symbol table. Just now, we extracted the name and age from the $data array, so that we can use $name $age in the view. For more details, please refer to http://www.php.net/manual/zh/function.extract.php
The function of the second ob_end_clean() is to close the top-level buffer, in order to clear some text that was accidentally echoed by the previous program, and to reopen a buffer for the next line.
The third ob_start() is to open a new buffer in order to put the content of the view into the buffer. Of course, the buffer has a certain size. If the content exceeds the buffer's set value, it will be automatically sent to the server.
The fourth require file, this is the first parameter, loads the view file according to its own rules. The files can contain php and html codes. Any local variables you declare in this render() function or any global variables accessible here can be accessed in the require file.
The fifth $content = ob_get_contents () is very important, it is to take out the contents of the buffer but not clear it.
The seventh ob_start() is to reopen a buffer, in order to use the buffer for the following program. If the writing framework may not need to operate on the contents of $content, then ob_end_flush() can directly output the contents of the buffer.
This is a very simple process of displaying views. If you use this directly, it is not convenient to modularize the view, so some frameworks will not use it so directly.
We can also see from this function that the program is somewhat similar to the program interrupt protection scene. It's just that the interruption protection site will save the data first, and then restore it when returning. Here we only have to close the previous buffer, open a new buffer, close this buffer, and open another buffer.
So far, we have seen a simple PHP MVC framework. If you are interested, you can develop an MVC framework yourself, or a more in-depth HMVC.