View中的查找视图文件方法和渲染文件方法

     /**
      * Finds the view file based on the given view name.
      * 通过view文件名查找view文件
      * @param string $view the view name or the path alias of the view file. Please refer to [[render()]]
      * on how to specify this parameter.
      * @param string $view 视图文件名
      * @param object $context the context to be assigned to the view and can later be accessed via [[context]]
      * in the view. If the context implements [[ViewContextInterface]], it may also be used to locate
      * the view file corresponding to a relative view name.
      * @param object $context 情景
      * @return string the view file path. Note that the file may not exist.
      * @return string view文件路径
      * @throws InvalidCallException if a relative view name is given while there is no active context to
      * determine the corresponding view file.
      */
     protected function findViewFile($view, $context = null)
     {
         if (strncmp($view, '@', 1) === 0) {//判断$view是否是别名路径
             // e.g. "@app/views/main"
             $file = Yii::getAlias($view);//调用getAlias()方法获取真实路径
         } elseif (strncmp($view, '//', 2) === 0) {//判断$view是否以'//'开始
             // e.g. "//layouts/main"
             //调用getViewPath()方法在当前模块下查找视图文件目录路径,然后拼接文件名合成视图文件路径
             $file = Yii::$app->getViewPath() . DIRECTORY_SEPARATOR . ltrim($view, '/');
         } elseif (strncmp($view, '/', 1) === 0) {//判断$view是否以 '/'开始
             // e.g. "/site/index"
             if (Yii::$app->controller !== null) {//且控制器存在
                 //则调用getViewPath()查找当前控制器对应的视图文件目录,然后拼接文件名合成视图文件路径
                 $file = Yii::$app->controller->module->getViewPath() . DIRECTORY_SEPARATOR . ltrim($view, '/');
             } else {//否则,抛出异常
                 throw new InvalidCallException("Unable to locate view file for view '$view': no active controller.");
             }
         } elseif ($context instanceof ViewContextInterface) {
             $file = $context->getViewPath() . DIRECTORY_SEPARATOR . $view;
         } elseif (($currentViewFile = $this->getViewFile()) !== false) {
             $file = dirname($currentViewFile) . DIRECTORY_SEPARATOR . $view;
         } else {
             throw new InvalidCallException("Unable to resolve view file for view '$view': no active view context.");
         }

         if (pathinfo($file, PATHINFO_EXTENSION) !== '') {//如果视图文件的扩展名不为空,直接返回扩展名
             return $file;
         }
         $path = $file . '.' . $this->defaultExtension;//给视图文件添加扩展名
         if ($this->defaultExtension !== 'php' && !is_file($path)) {//这里应该是做二次校验,添加扩展名
             $path = $file . '.php';
         }

         return $path;
     }

     /**
      * Renders a view file.
      * 渲染一个view文件
      *
      * If [[theme]] is enabled (not null), it will try to render the themed version of the view file as long
      * as it is available.
      * 如果[theme]可用(即非空),该方法将视图渲染themed version的视图文件直到[theme]不可用
      * The method will call [[FileHelper::localize()]] to localize the view file.
      * 该方法将调用[FileHelper::localize()]方法本地化视图文件
      *
      * If [[renderers|renderer]] is enabled (not null), the method will use it to render the view file.
      * Otherwise, it will simply include the view file as a normal PHP file, capture its output and
      * return it as a string.
      * 如果[renderers|renderer]可用(即非空),该方法将渲染视图文件,否则将把视图文件作为常规的php文件包含进来
      * 捕捉它的输出并将其返回为字符串
      *
      * @param string $viewFile the view file. This can be either an absolute file path or an alias of it.
      * @param array $params the parameters (name-value pairs) that will be extracted and made available in the view file.
      * @param object $context the context that the view should use for rendering the view. If null,
      * existing [[context]] will be used.
      * @return string the rendering result
      * @throws InvalidParamException if the view file does not exist
      */
     public function renderFile($viewFile, $params = [], $context = null)
     {
         $viewFile = Yii::getAlias($viewFile);//调用getAlias()方法处理输入的视图文件名

         if ($this->theme !== null) {
             $viewFile = $this->theme->applyTo($viewFile);//如果theme非空,怎将其应用到视图文件
         }
         if (is_file($viewFile)) {
             $viewFile = FileHelper::localize($viewFile);//调用[FileHelper::localize()]方法本地化视图文件
         } else {//$viewFile不是文件,抛出异常“视图文件不存在”
             throw new InvalidParamException("The view file does not exist: $viewFile");
         }

         $oldContext = $this->context;
         if ($context !== null) {
             $this->context = $context;
         }
         $output = '';
         $this->_viewFiles[] = $viewFile;

         if ($this->beforeRender($viewFile, $params)) {//如果renderFile的前置事件执行成功
             Yii::trace("Rendering view file: $viewFile", __METHOD__);//记录trace信息
             $ext = pathinfo($viewFile, PATHINFO_EXTENSION);//获取视图文件扩展名
             if (isset($this->renderers[$ext])) {//判断视图文件的扩展名是否支持
                 if (is_array($this->renderers[$ext]) || is_string($this->renderers[$ext])) {
                     $this->renderers[$ext] = Yii::createObject($this->renderers[$ext]);
                 }
                 /* @var $renderer ViewRenderer */
                 $renderer = $this->renderers[$ext];//如果支持的,将view渲染器对象赋值给$renderer
                 $output = $renderer->render($this, $viewFile, $params);//渲染视图文件
             } else {//如果视图文件不是支持的类型,则以普通php文件处理
                 $output = $this->renderPhpFile($viewFile, $params);
             }
             $this->afterRender($viewFile, $params, $output);//调用renderFile的后置方法
         }

         array_pop($this->_viewFiles);//删除viewFiles中最后面的一个元素
         $this->context = $oldContext;

         return $output;
     }

Yii源码阅读笔记(十八)的更多相关文章

  1. Yii源码阅读笔记(八)

    前面阅读了Yii2的两个基本类Object和Component,了解了Yii的三个重要概念属性.事件.行为,下面开始阅读Event类,Event类是所有事件类的基类: <?php /** * @ ...

  2. Yii源码阅读笔记(一)

    今天开始阅读yii2的源码,想深入了解一下yii框架的工作原理,同时学习一下优秀的编码规范和风格.在此记录一下阅读中的小心得. 每个框架都有一个入口文件,首先从入口文件开始,yii2的入口文件位于we ...

  3. Yii源码阅读笔记(二十八)

    Yii/web中的Controller类,实现参数绑定,启动csrf验证功能,重定向页面功能: namespace yii\web; use Yii; use yii\base\InlineActio ...

  4. Yii源码阅读笔记(三十五)

    Container,用于动态地创建.注入依赖单元,映射依赖关系等功能,减少了许多代码量,降低代码耦合程度,提高项目的可维护性. namespace yii\di; use ReflectionClas ...

  5. Yii源码阅读笔记(三十四)

    Instance类, 表示依赖注入容器或服务定位器中对某一个对象的引用 namespace yii\di; use Yii; use yii\base\InvalidConfigException; ...

  6. Yii源码阅读笔记(三十二)

    web/Application类的注释,继承base/Application类,针对web应用的一些处理: namespace yii\web; use Yii; use yii\base\Inval ...

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

    Widget类是所有小部件的基类,开始,结束和渲染小部件内容的方法的注释: namespace yii\base; use Yii; use ReflectionClass; /** * Widget ...

  8. Yii源码阅读笔记(二十九)

    动态模型DynamicModel类,用于实现模型内数据验证: namespace yii\base; use yii\validators\Validator; /** * DynamicModel ...

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

    Application 类中设置路径的方法和调用ServiceLocator(服务定位器)加载运行时的组件的方法注释: /** * Handles the specified request. * 处 ...

随机推荐

  1. watchdog机制

    转自:http://blog.sina.com.cn/s/blog_4dff871201012yzh.html 什么是Watchdog? Watchdog,又称watchdog timer,是计算机可 ...

  2. oracle的关闭过程(各个模式关闭)

    关闭数据库与实例 与数据库启动一下,关闭数据库与实例也分为3步:关闭数据库-->实例卸载数据库--->终止实例. 1.Nomal(正常关闭方式) 命令:shutdown nomal 讲解: ...

  3. Oracle 11g必须开启的服务及服务详细介绍(转)

    成功安装Oracle 11g数据库后,你会发现自己电脑运行速度会变慢,配置较低的电脑甚至出现非常卡的状况,通过禁止非必须开启的Oracle服务可以提升电脑的运行速度.那么,具体该怎么做呢? 按照win ...

  4. AppInventor学习笔记(三)——油漆桶应用学习

    一.组件设置 1.画笔颜色选项 选取3个Button,然后改名为红.黄.绿三种颜色,然后进行相应属性的设置. 在这里有个问题就是如何放在一行.. 将3个按钮放进这个方框里面就可以变成一行了. 2.画布 ...

  5. synchronized的理解

    用法解释 synchronized是Java中的关键字,是一种同步锁.它修饰的对象有以下几种: 1. 修饰一个代码块,被修饰的代码块称为同步语句块,其作用的范围是大括号{}括起来的代码,作用的对象是调 ...

  6. TODO:C# Socket

    http://www.cnblogs.com/licongjie/archive/2006/10/26/540640.html http://blog.csdn.net/ZOU_SEAFARER/ar ...

  7. bzoj1019 [SHOI2008]汉诺塔

    1019: [SHOI2008]汉诺塔 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 1030  Solved: 638[Submit][Status] ...

  8. BZOJ2675 : Bomb

    首先通过不断翻转坐标系,假设三个点以横坐标为第一关键字,纵坐标为第二关键字排序后A在B前面,B在C前面. 那么只需要处理以下两种情况: 1.B的纵坐标在AC之间,这时三个点的距离和为$2((x_C+y ...

  9. BZOJ2051 : A Problem For Fun

    树的点分治,将点分治的过程记录下来,每一个分治结构按到分治中心的距离维护所有点. 对于一个点二分答案,然后在$O(\log n)$个分治结构中二分查找,时间复杂度$O(n\log^3n)$. #inc ...

  10. webkit webApp 开发技术要点总结【转】

    如果你是一名前端er,又想在移动设备上开发出自己的应用,那怎么实现呢?幸好,webkit内核的浏览器能帮助我们完成这一切.接触 webkit webApp的开发已经有一段时间了,现把一些技巧分享给大家 ...