Yii源码阅读笔记(十七)

View.php,继承了component,用于渲染视图文件:

  1 namespace yii\base;
  2
  3 use Yii;
  4 use yii\helpers\FileHelper;
  5 use yii\widgets\Block;
  6 use yii\widgets\ContentDecorator;
  7 use yii\widgets\FragmentCache;
  8
  9 /**
 10  * View represents a view object in the MVC pattern.
 11  *
 12  * View provides a set of methods (e.g. [[render()]]) for rendering purpose.
 13  * 视图提供了一套渲染页面的方法(例如[render()])
 14  *
 15  * @property string|boolean $viewFile The view file currently being rendered. False if no view file is being
 16  * rendered. This property is read-only.
 17  *
 18  * @author Qiang Xue <[email protected]>
 19  * @since 2.0
 20  */
 21 class View extends Component
 22 {
 23     /**
 24      * @event Event an event that is triggered by [[beginPage()]].
 25      * beginPage事件名常量,被[beginPage()]触发
 26      */
 27     const EVENT_BEGIN_PAGE = ‘beginPage‘;
 28     /**
 29      * @event Event an event that is triggered by [[endPage()]].
 30      * endPage事件名常量,被[endPage()]触发
 31      */
 32     const EVENT_END_PAGE = ‘endPage‘;
 33     /**
 34      * @event ViewEvent an event that is triggered by [[renderFile()]] right before it renders a view file.
 35      * beforeRender事件名常量,被[renderFile()]触发
 36      */
 37     const EVENT_BEFORE_RENDER = ‘beforeRender‘;
 38     /**
 39      * @event ViewEvent an event that is triggered by [[renderFile()]] right after it renders a view file.
 40      * afterRender事件名常量,被[renderFile()]触发
 41      */
 42     const EVENT_AFTER_RENDER = ‘afterRender‘;
 43
 44     /**
 45      * @var ViewContextInterface the context under which the [[renderFile()]] method is being invoked.
 46      */
 47     public $context;
 48     /**
 49      * @var mixed custom parameters that are shared among view templates.
 50      * 在视图模板中共享的自定义参数
 51      */
 52     public $params = [];
 53     /**
 54      * @var array a list of available renderers indexed by their corresponding supported file extensions.
 55      * Each renderer may be a view renderer object or the configuration for creating the renderer object.
 56      * 一个可用的渲染索引列表。每个渲染器是一个渲染器对象或创建渲染对象配置数组
 57      * For example, the following configuration enables both Smarty and Twig view renderers:
 58      * 下面的配置数组同时支持 Smarty 和 Twig
 59      *
 60      * ```php
 61      * [
 62      *     ‘tpl‘ => [‘class‘ => ‘yii\smarty\ViewRenderer‘],
 63      *     ‘twig‘ => [‘class‘ => ‘yii\twig\ViewRenderer‘],
 64      * ]
 65      * ```
 66      *
 67      * If no renderer is available for the given view file, the view file will be treated as a normal PHP
 68      * and rendered via [[renderPhpFile()]].
 69      * 如果没有值,则
 70      */
 71     public $renderers;
 72     /**
 73      * @var string the default view file extension. This will be appended to view file names if they don‘t have file extensions.
 74      * 默认视图文件扩展名,在视图文件没有扩展名的情况下自动添加
 75      */
 76     public $defaultExtension = ‘php‘;
 77     /**
 78      * @var Theme|array|string the theme object or the configuration for creating the theme object.
 79      * If not set, it means theming is not enabled.
 80      * 主题对象或创建主题对象的配置
 81      */
 82     public $theme;
 83     /**
 84      * @var array a list of named output blocks. The keys are the block names and the values
 85      * are the corresponding block content. You can call [[beginBlock()]] and [[endBlock()]]
 86      * to capture small fragments of a view. They can be later accessed somewhere else
 87      * through this property.
 88      * 一个输出块列表。键是块名称值为相应的块内容。你可以调用 [ beginblock() ]和[ endblock() ]捕获视图的small fragments
 89      * 它们可以在其他地方通过这个属性访问。
 90      */
 91     public $blocks;
 92     /**
 93      * @var array a list of currently active fragment cache widgets. This property
 94      * is used internally to implement the content caching feature. Do not modify it directly.
 95      * 当前active fragment缓存小部件的列表。此属性用于内部实现内容缓存功能。不要直接修改它。
 96      * @internal
 97      */
 98     public $cacheStack = [];
 99     /**
100      * @var array a list of placeholders for embedding dynamic contents. This property
101      * is used internally to implement the content caching feature. Do not modify it directly.
102      * 一种嵌入动态内容占位符列表。此属性用于内部实现内容缓存功能。不要直接修改它。
103      * @internal
104      */
105     public $dynamicPlaceholders = [];
106
107     /**
108      * @var array the view files currently being rendered. There may be multiple view files being
109      * rendered at a moment because one view may be rendered within another.
110      * 目前正在渲染的视图文件。可能有多个视图文件被渲染,因为一个视图可以在另一个视图中呈现
111      */
112     private $_viewFiles = [];
113
114
115     /**
116      * Initializes the view component.
117      * 初始化视图组件
118      */
119     public function init()
120     {
121         parent::init();//调用父类的init方法
122         if (is_array($this->theme)) {//如果theme是数组
123             if (!isset($this->theme[‘class‘])) {//且没有设置类名
124                 $this->theme[‘class‘] = ‘yii\base\Theme‘;//则类名为‘yii\base\Theme‘
125             }
126             //以配置的形式创建对象
127             $this->theme = Yii::createObject($this->theme);
128         } elseif (is_string($this->theme)) {//否则以字符串参数的形式创建
129             $this->theme = Yii::createObject($this->theme);
130         }
131     }
132
133     /**
134      * Renders a view.
135      * 渲染一个视图
136      * The view to be rendered can be specified in one of the following formats:
137      * 被渲染的视图可以用下列方式指定
138      *
139      * - path alias (e.g. "@app/views/site/index");
140      *   路径别名
141      * - absolute path within application (e.g. "//site/index"): the view name starts with double slashes.
142      *   The actual view file will be looked for under the [[Application::viewPath|view path]] of the application.
143      *   绝对路径,将会在[Application::viewPath|view path]下查找文件
144      * - absolute path within current module (e.g. "/site/index"): the view name starts with a single slash.
145      *   The actual view file will be looked for under the [[Module::viewPath|view path]] of the [[Controller::module|current module]].
146      *   模块下的绝对路径,将会在[Module::viewPath|view path]下查找文件
147      * - relative view (e.g. "index"): the view name does not start with `@` or `/`. The corresponding view file will be
148      *   looked for under the [[ViewContextInterface::getViewPath()|view path]] of the view `$context`.
149      *   相对路径,将会在[ViewContextInterface::getViewPath()|view path]下查找文件
150      *   If `$context` is not given, it will be looked for under the directory containing the view currently
151      *   being rendered (i.e., this happens when rendering a view within another view).
152      *
153      * @param string $view the view name.
154      * @param array $params the parameters (name-value pairs) that will be extracted and made available in the view file.
155      * @param object $context the context to be assigned to the view and can later be accessed via [[context]]
156      * in the view. If the context implements [[ViewContextInterface]], it may also be used to locate
157      * the view file corresponding to a relative view name.
158      * @return string the rendering result
159      * @throws InvalidParamException if the view cannot be resolved or the view file does not exist.
160      * @see renderFile()
161      */
162     public function render($view, $params = [], $context = null)
163     {
164         //查找视图文件路径
165         $viewFile = $this->findViewFile($view, $context);
166         //调用renderFile()渲染视图文件
167         return $this->renderFile($viewFile, $params, $context);
168     }
时间: 2024-10-27 13:00:46

Yii源码阅读笔记(十七)的相关文章

Yii源码阅读笔记 - 日志组件

?使用 Yii框架为开发者提供两个静态方法进行日志记录: Yii::log($message, $level, $category);Yii::trace($message, $category); 两者的区别在于后者依赖于应用开启调试模式,即定义常量YII_DEBUG: defined('YII_DEBUG') or define('YII_DEBUG', true); Yii::log方法的调用需要指定message的level和category.category是格式为“xxx.yyy.z

Yii源码阅读笔记(一)

今天开始阅读yii2的源码,想深入了解一下yii框架的工作原理,同时学习一下优秀的编码规范和风格.在此记录一下阅读中的小心得. 每个框架都有一个入口文件,首先从入口文件开始,yii2的入口文件位于web目录的index.php,用于启动web应用和配置一些路径参数. index.php—— 1 // comment out the following two lines when deployed to production 2 defined('YII_DEBUG') or define('Y

Yii源码阅读笔记(二十七)

Theme 类,即一个应用的主题,主要通过替换路径实现主题的应用,里边的方法为获取根路径和根链接,以及应用主题的方法: 1 namespace yii\base; 2 3 use Yii; 4 use yii\helpers\FileHelper; 5 6 /** 7 * Theme represents an application theme. 8 * Theme 类,即一个应用的主题 9 * 10 * When [[View]] renders a view file, it will c

Yii源码阅读笔记(二)

接下来阅读BaseYii.php vendor/yiisoft/yii2/Yii.php—— 1 namespace yii; 2 3 use yii\base\InvalidConfigException; 4 use yii\base\InvalidParamException; 5 use yii\base\UnknownClassException; 6 use yii\log\Logger; 7 use yii\di\Container; 第1行定义命名空间为yii: 第3到7行使用了

Yii源码阅读笔记(三)

接着上次的继续阅读BaseYii.php vendor/yiisoft/yii2/BaseYii.php—— 1 public static function getRootAlias($alias)//获取根别名 2 { //查找别名中斜线的位置 3 $pos = strpos($alias, '/'); //根据斜线的结果判断,如果不包含斜线,表示输入为根别名,否则截取斜线前面的部分作为根别名 4 $root = $pos === false ? $alias : substr($alias

Yii源码阅读笔记(八)

前面阅读了Yii2的两个基本类Object和Component,了解了Yii的三个重要概念属性.事件.行为,下面开始阅读Event类,Event类是所有事件类的基类: 1 <?php 2 /** 3 * @link http://www.yiiframework.com/ 4 * @copyright Copyright (c) 2008 Yii Software LLC 5 * @license http://www.yiiframework.com/license/ 6 */ 7 8 nam

Yii源码阅读笔记(三十三)

ServiceLocator,服务定位类,用于yii2中的依赖注入,通过以ID为索引的方式缓存服务或则组件的实例来定位服务或者组件: 1 namespace yii\di; 2 3 use Yii; 4 use Closure; 5 use yii\base\Component; 6 use yii\base\InvalidConfigException; 7 8 /** 9 * ServiceLocator implements a [service locator](http://en.wi

Yii源码阅读笔记(二十一)——请求处理流程

Yii2请求处理流程: 首先:项目路径/web/index.php (new yii\web\Application($config))->run();//根据配置文件创建App实例,先实例化yii\web\Application(),然后调用run()方法 该语句可分为两步: $application = new yii\web\Application($config);//实例化app $application->run();//调用run()方法 $config 为配置文件,通过 req

Yii源码阅读笔记(二十六)

Application 类中设置路径的方法和调用ServiceLocator(服务定位器)加载运行时的组件的方法注释: 1 /** 2 * Handles the specified request. 3 * 处理指定的请求--抽象方法 4 * This method should return an instance of [[Response]] or its child class 5 * which represents the handling result of the reques