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.
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:
render()
method on the View component is called and the $view
and $params
arguments are passed to it.$view
argument.render()
method is called, and the path to the view file, the View component instance, and the original $params
arguments are passed to it.require()
method mechanics.yiiwebResponse
class, such an instance is created and the rendering result is passed to it as the data
attribute.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.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.Response
instance can process the given format itself, it does so by modifying its headers and content.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:
index.php
file is checked for its own existence at the following location <root_directory>/<views_directory>/<controller_id>/
.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.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.Content-Type: text/html; charset=<yii application charset>
header is sent to the client.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:
render()
method so that Yii will find it?Before we talk about the answers to these questions, an explanation about the concept of Yii application components will be useful.
52.15.176.80