They should be more interested in how they work, and let me say what an MVC framework looks like.
Routing mechanism
In the Internet we are all through the URL to provide services, so different URLs have different services. Users can access different pages and get different services. So how does our service differentiate different services by URL?
Our web program is going to find different files through the URL, and do different business logic processing. Our routing mechanism is based on the URL to find the corresponding controller, and action, and then the action for the specific business logic processing.
A simple controller.
Copy Code code as follows:
Define a controller
Class Usercontroler extends controller{
Define an action method, and note that it must be public.
Public Function index () {
Do business code
}
}
The specific corresponding rules vary from frame to map. The following is the URL route for the CodeIgniter framework, which tries to try various possibilities to parse the URL.
File path/system/core/uri.php
Copy Code code as follows:
To see if it was run from the command line.
if (php_sapi_name () = = ' CLI ' or defined (' STDIN ')} {
$this->_set_uri_string ($this->_parse_cli_args ());
Return
}
First try to request_uri this adaptation to most of the situation
if ($uri = $this->_detect_uri ()) {
$this->_set_uri_string ($uri);
Return
}
See if the PATH_INFO variable exists? Nginx needs to be configured
Note:some servers seem to have trouble with getenv () so we ' ll test it two ways
$path = (isset ($_server[' path_info '))? $_server[' path_info ': @getenv (' path_info ');
if (Trim ($path, '/')!= ' && $path!= '/'. SELF) {
$this->_set_uri_string ($path);
Return
}
No path_info, see query_string?
$path = (isset ($_server[' query_string '))? $_server[' query_string ': @getenv (' query_string ');
if (Trim ($path, '/')!= ') {
$this->_set_uri_string ($path);
Return
}
Try to get information from $_get
if (Is_array ($_get) && count ($_get) = = 1 && trim (key ($_get), '/')!= ') {
$this->_set_uri_string (Key ($_get));
Return
}
Tried, gave up the route
$this->uri_string = ';
Return
With the above attempt, the next step is how to use the routing mechanism to load the correct controller.
Controller loading mechanism
Let's see how the CodeIgniter frame loads into controller and invokes the action.
The following code is available in the/system/core/codeigniter.php. CodeIgniter before this will be based on the value of $_server[' path_info] to be assigned (this is by their own settings, the default CI he will have a lot of if branch to judge).
Copy Code code as follows:
It's about 250 lines.
Include (AppPath. ' controllers/'. $RTR->fetch_directory (). $RTR->fetch_class (). php ');
$class = $RTR->fetch_class ();
$method = $RTR->fetch_method ();
It's about 308 lines.
$CI = new $class ();
It's about 359 lines.
Call_user_func_array (Array (& $CI, $method), Array_slice ($URI->rsegments, 2));
In this way, this is called to our controller and its methods, and next is to write their own business logic code.
Display of Views View
When our business logic code is finished, we need the display of the page. Many of the common MVC frameworks are written in the page call.
Copy Code code as follows:
The method of action in controller
Public Function index () {
// ... A lot of business logic code
$data = Array (' name ' => ' abc ', ' Age ' =>12, ...);
return $this->render (' view/path/file.html ', $data);
}
Then write the code in the view file view/path/file.html.
Copy Code code as follows:
<div>
Name: <?= $name?>
Age: <?php echo $age;?>
</div>
This section of how to render the data into view, which I have been curious about before, and now I understand, let's see how it's done.
Copy Code code as follows:
protected function render ($template, array $var = Array ())
{
Extract ($var); To extract a variable from an array
Ob_end_clean (); Turn off top-level output buffer content
Ob_start (); Start a new buffer
Require Template_root. $template. '. html '; Load views View
$content = Ob_get_contents (); Get the contents of the buffer
Ob_end_clean (); Close buffer
Ob_end_flush (); This is the content of the direct output buffer, not to be cached again.
Ob_start (); Starts a new buffer, which is used by the following program
return $content; Returns the text, where the byte echo comes out and ends the code.
}
In this short line of code, all the essence, is these very important, are all PHP built-in functions, and then we analyze the specific analysis.
Take a look at the first extract ($var). This function imports variables from the array into the current symbol table. You have just extracted the name and age of the $data array so that you can use the $name $age in view views. Please refer to http://www.php.net/manual/zh/function.extract.php for more details
The second Ob_end_clean () function is to close the top buffer, in order to the previous program inadvertently echo out some of the text to clear, for the next line to reopen a buffer.
The third Ob_start () is to open a new buffer in order to put the contents of the view into the buffer. Of course, the buffer has a certain size, and if the content exceeds the set value of the buffer, it is automatically sent to the server.
The fourth require file, which is the first argument, loads the view's files according to its own rules. In which files can be mixed with PHP, HTML code. Any local variables you declare in this render () function, or any global variables that you can access here, can be accessed in require file files.
The Fifth $content = Ob_get_contents () is important in order to remove the contents of the buffer, but not clear it.
The seventh Ob_start () is to reopen a buffer so that the following program needs to use a buffer. The writing framework may not operate on $content content, so direct ob_end_flush () outputs the contents of the buffer.
This is a very simple process of showing the view. Some frameworks are less straightforward to use if this is not convenient for modularity of view views.
We can also see from this function that the program is somewhat similar to the way the program interrupts the protection of the scene. Only the interruption of the protection site will save the data, and then return back to the time of recovery. This only closes the previous buffer, opens a new buffer, and closes the buffer. In the past, open another buffer.
So far, we see a simple MVC framework for PHP. If you are interested in developing an MVC framework, or a deeper point of HMVC.