If you want to see the view implementation of the Yii framework, keep going; If you want to read the fat man’s babble, skip to the end of the article; If you just pass by, then pass by too.
The key points of the MVC pattern mentioned by Martin Flower in The Enterprise Application Architecture Pattern are two separations: the separation of the view from the model and the separation of the controller from the view. The presentation of the view largely determines the use of this pattern and the level of separation the framework has from the various levels of the MVC pattern. Yii framework is a framework based on MVC pattern. It has done a lot of work in view and clearly realized the function of view.
The view of the Yii framework is a PHP script that contains the major elements of user interaction. Each view has a name, which is used to identify the view script file when rendering. The name of the view is the same as its view script name. For example, the view Edit name comes from a script file called edit.php. To render, call CController:: Render () by passing the name of the view. This method will find the corresponding view file in the “protected/ Views/Controller ID” directory by using getViewFile. Protected /views is the default storage location, which can be changed by Yii::app()->setViewPath.
Inside the view script, we can access the controller instance through $this. We can also get any controller property in the view as “$this-> property name” by implementing the __get magic method.
The render function of the CController class provides a more complete view rendering process. When the controller gets the data from the model, it usually executes the Render () method to create the view, which looks like this:
- Execute the reserved beforeRender hook.
- $output, in fact, here is a partial view rendering process, it includes obtaining view file path, rendering view file, processing output three parts. During rendering, use the EXTRACT () method in PHP to import variables from the array into the current symbol table, requiring the view file directly to merge data and presentation.
- If a layout exists, the resulting content is passed to the parent class’s renderFile() method in an array with content as the subscript, and the rendering process is repeated. Execute in layout, output local content $output, realize local and layout view merge. In order to achieve multi-level layout, the layout can also be loaded through the controller’s view decorator method.
- Execute the reserved afterRender hook.
When rendering a view, the processOutput() method is executed if a value is passed in the argument. This method is usually called at the end of the rendering view. It implements three procedures:
- Registers client scripts, managed by the ClientScript component.
- If so, dynamic content output is performed.
- Page content base64_encode is encrypted and compressed first if a Zlib extension exists.
There are several other ways to render a view in the CController class besides the render method above:
- $view,$data=null,$return=false
- RenderPartial: Render only the view content, or render part of the page content. It differs from the Render () method in that it does not render the layout and is also called in the Render () method. renderPartial($view,$data=null,$return=false,$processOutput=false)
- RenderText method: Render static content and layout. renderText($text,$return=false)
- RenderDynamic method: renderDynamic content with a callback function, usually called in a template file. renderDynamic($callback)->renderDynamicInternal($callback,$params)
- RenderClip method: Render the content generated by the CClipWidget with a name. renderClip($name,$params=array(),$return=false)
Although it is possible to renderPartial page views with the renderPartial method for content shared by different pages, there must be duplication of data parts because these views need to call the data provided by the control, resulting in coupling. So the Yii framework provides another separate view Widget, officially called a Widget. Small widgets?) .
A small object is an instance of a CWidget or its subclasses. It is a component that is primarily used to represent data. Small objects are often embedded in a view to produce complex and independent user interfaces. It is also a kind of interface independence and loose coupling design. For example, we often use lists, pages, calendars and so on to make WEB applications. These widgets increase interface reuse and reduce code.
Unlike the previous view section, there is no layout file support, and $this in the Widget view points to the Widget instance instead of the controller instance, which is separated from the controller. If you want to implement a custom Widget, you simply inherit the CWidget and override its init() and run() methods. You can define a new Widget.
We call the widget in the view via $this-> Widget() or $this->beginWidget() and $this->endWidget(). The difference between the two is that the second method adds HTML content during display. The place to add content is between the init() method and the output of the run() method.
In addition to layout and widgets, the Yii framework implements system-level views that display Yii error and log information.
The naming of system views follows a few rules. For example, names like “errorXXX” are used to render views that show CHttpException with error number XXX. In framework/ Views, Yii provides a series of default system views. You can customize this by creating a view file with the same name under protected/views/system. The default exception view, combined with Yii’s own traces mechanism, is a great way to pinpoint the correct code when an exception or error is thrown.
The above is just a note of fat man reading the source code of Yii framework. Combining the content of the book Enterprise Application Architecture Patterns, such as page Controllers, front-end controllers, and activity logging, Fatty found a deeper understanding of the implementation of frameworks. On the one hand, it confirms the theory in the book, and on the other hand, it finds the source for the principle of the realization process. I wonder if the author of the Yii framework has also read this book intensively, or accumulated experience?