Chapter 4. The Renderer

Towards the end of the last chapter, we were forced to jump over our heads and touch the view code. This chapter will basically be an explanation of what we really did then.

Despite the name of this chapter, there's no such thing as a separate Renderer object in Yii. Being an MVC-based framework, Yii employs an entire set of processes that perform the rendering. These processes are spread through the whole code base.

Anatomy of Yii rendering

When a web application visitor's request is being handled, there are a few processing steps your data goes through before it is sent to the visitor's browser:

  1. A controller action is run. It will use the render() method to process a PHP script (sending it some parameters if necessary) and get HTML form from it to send to the client browser. Please note that this action is voluntary, not mandatory. You can have controller actions that do not call the render() method at all.
  2. The render() method on the View component is called and the $view and $params arguments are passed to it.
  3. The View component figures out the view file to be used with the help of the path alias passed in the $view argument.
  4. The View component checks whether it has any view renderers associated with the filename extension of the view file it has just found.
  5. If there's indeed such a view renderer, its render() method is called, and the path to the view file, the View component instance, and the original $params arguments are passed to it.
  6. If there's no such view renderer, the view file is processed using the traditional PHP require() method mechanics.
  7. If the result of the rendering is not an instance of the yiiwebResponse class, such an instance is created and the rendering result is passed to it as the data attribute.
  8. The instance of Response looks at the value of its format attribute and checks whether it has custom ResponseFormatterInterface implementers associated with this value. The value of the format attribute can also be one of the built-in formats, which can be handled by the Response instance itself.
  9. If indeed there is such a response formatter, its format() method is called, and the whole Response instance is passed to it so that format() can properly set the headers and content of the Response instance.
  10. If the Response instance can process the given format itself, it does so by modifying its headers and content.
  11. Finally, the headers are sent using the standard PHP header() mechanics, and the content is just flushed down the pipe to the client's browser.

The view renderer concept is implemented in Yii 2, unsurprisingly, by the yiiaseViewRenderer abstract class. The response formatter is implemented by the yiiwebResponseFormatterInterface interface.

In the most rudimentary case, where you don't have any themes, tweak the view renderers or response formatters, and execute the following in your controller:

$this->render('index', ['dataProvider' => $dataProvider]);

When the preceding line of code is executed, the following happens:

  1. The index.php file is checked for its own existence at the following location <root_directory>/<views_directory>/<controller_id>/.
  2. Inside the View component, the extract(['dataProvider' => $dataProvider]) action happens. As a result, the value stored in the dataProvider key becomes accessible as the $dataProvider variable in the current scope.
  3. The require("<root_directory>/<views_directory>/<controller_id>/index.php") method is run inside the ob_start()ob_get_clean() pair, so all of its output is captured as a string.
  4. The Content-Type: text/html; charset=<yii application charset> header is sent to the client.
  5. Finally, the return value of the render() method is sent to the client verbatim.

Now, to know the intricate details of how render() does its job, we can look into both the documentation and the source code of the yiiaseController.render() method. What we are really interested in are four simple questions:

  • How do we specify the first argument of the render() method so that Yii will find it?
  • How does Yii determine what layout is to be used and how we can set it explicitly?
  • Can we use other template formats as view files?
  • Can we send the rendering result to the client in some unusual way? JSON being the most obvious example.

Before we talk about the answers to these questions, an explanation about the concept of Yii application components will be useful.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.60.63