Home  >  Article  >  Backend Development  >  PHP study notes (4)_PHP tutorial

PHP study notes (4)_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:34:56823browse

It is very simple to implement a so-called MVC framework in PHP. Let’s talk about the idea here.

1. First, use the spl_autoload_register function to implement your own type automatic loading mechanism. In this way, other codes no longer need to consider how to include the file where the class is located.

2. Extract the controller and action names from the URL, organize them to use reflection to instantiate the controller class, and call the controller's execute method. The execute method of the controller will also use reflection to find the action method and call it, and write the response object returned by the action to the output stream. If the controller or action does not exist, a 404 status is returned.

3. To implement a view, you must first define a view class, which includes another php view file into a method, which can limit the scope of variables in the included file. Secondly, the view class will have a $model attribute, which is used to share the data model with the included php, so that we can easily present data in the view. Of course, you can also use the functions ob_start, ob_get_contents, and ob_end_clean to "capture" the content presented by the view.

Fourth, continue with the second step. If you want to present a view, then return a ViewResponse in the action. If you want to output a piece of json data, return a JsonResponse. The framework provides a base class ActionResponse, no matter what its derived class is. The framework only needs to write what it generates to the output.

You need to pay attention to safety and performance issues when using it in practice.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/748241.htmlTechArticleIt is very simple to implement a so-called MVC framework in PHP. Let’s talk about the idea here. 1. First, use the spl_autoload_register function to implement your own type automatic loading mechanism. In this way, other codes...
Statement:
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