controller类的render部分,用于渲染视图和布局文件:

 /**
      * Returns all ancestor modules of this controller.
      * 获取当前控制器所有的父模块
      * The first module in the array is the outermost one (i.e., the application instance),
      * while the last is the innermost one.
      * @return Module[] all ancestor modules that this controller is located within.
      */
     public function getModules()
     {
         $modules = [$this->module];
         $module = $this->module;
         while ($module->module !== null) {
             //由这里可知,返回的数组顺序为从父模块到子模块
             array_unshift($modules, $module->module);
             $module = $module->module;
         }
         return $modules;
     }

     /**
      * @return string the controller ID that is prefixed with the module ID (if any).
      *  返回控制器id
      */
     public function getUniqueId()
     {
        //如果当前所属模块为application,则就为该id,否则要前面要加上模块id
         return $this->module instanceof Application ? $this->id : $this->module->getUniqueId() . '/' . $this->id;
     }

     /**
      * Returns the route of the current request.
      * //获取默认请求的路由信息
      * @return string the route (module ID, controller ID and action ID) of the current request.
      */
     public function getRoute()
     {
         return $this->action !== null ? $this->action->getUniqueId() : $this->getUniqueId();
     }

     /**
      * Renders a view and applies layout if available.
      * 渲染视图文件和布局文件(如果有布局的话)
      * The view to be rendered can be specified in one of the following formats:
      *
      * - 别名路径 (e.g. "@app/views/site/index");
      * - 应用内的视图:直接指定模板文件路径,以双斜线“//”开始的路径 (e.g. "//site/index"):
      *   The actual view file will be looked for under the [[Application::viewPath|view path]] of the application.
      * - 模块内的视图:直接指定模板文件路径,以单个斜线“/”开始的路径 (e.g. "/site/index"):
      *   The actual view file will be looked for under the [[Module::viewPath|view path]] of [[module]].
      * - relative path (e.g. "index"): the actual view file will be looked for under [[viewPath]].
      *
      * To determine which layout should be applied, the following two steps are conducted:
      * 确定应用布局文件类型的步骤:
      * 1. In the first step, it determines the layout name and the context module:
      * 首先确定布局文件名和背景模块
      * - If [[layout]] is specified as a string, use it as the layout name and [[module]] as the context module;
      * 如果布局文件是字符串,也就是设置布局文件,则直接调用
      * - If [[layout]] is null, search through all ancestor modules of this controller and find the first
      * 如果没有设置布局文件,则查找所有的父模块的布局文件。
      *   module whose [[Module::layout|layout]] is not null. The layout and the corresponding module
      *   are used as the layout name and the context module, respectively. If such a module is not found
      *   or the corresponding layout is not a string, it will return false, meaning no applicable layout.
      *
      * 2. In the second step, it determines the actual layout file according to the previously found layout name
      *    and context module. The layout name can be:
      *应用下的布局文件,以“/”开头,这个会从应用程序的布局文件目录下面查找布局文件
          * 3、其它情况,   这个会从当前模块的布局文件目录下查查找布局文件
      * - 别名路径(e.g. "@app/views/layouts/main");
      * - 绝对路径 (e.g. "/main"): 应用下的布局文件,以“/”开头,这个会从应用程序的布局文件目录下面查找布局文件
      * - 相对路径 (e.g. "main"): 这个会从当前模块的布局文件目录下查查找布局文件
      *   [[Module::layoutPath|layout path]] of the context module.
      *
      * If the layout name does not contain a file extension, it will use the default one `.php`.
      * 如果布局文件没有扩展名,则默认为.php
      * @param string $view the view name.
      * @param array $params the parameters (name-value pairs) that should be made available in the view.
      * These parameters will not be available in the layout.
      * @return string the rendering result.
      * @throws InvalidParamException if the view file or the layout file does not exist.
      */

     public function render($view, $params = [])
     {
         //由view对象渲染视图文件
         $content = $this->getView()->render($view, $params, $this);
         //调用renderContent方法渲染布局文件
         return $this->renderContent($content);
     }

     /**
      * Renders a static string by applying a layout.
      * 配合render方法渲染布局文件
      * @param string $content the static string being rendered
      * @return string the rendering result of the layout with the given static string as the `$content` variable.
      * If the layout is disabled, the string will be returned back.
      * @since 2.0.1
      */
     public function renderContent($content)
     {
         //查找布局文件
         $layoutFile = $this->findLayoutFile($this->getView());
         if ($layoutFile !== false) {
              //由view对象渲染布局文件,并把上面的视图结果作为content变量传递到布局中,所以布局中才会有$content变量来表示
             return $this->getView()->renderFile($layoutFile, ['content' => $content], $this);
         } else {
             return $content;
         }
     }

     /**
      * Renders a view without applying layout.
      * 这个只渲染视图文件,不会应用布局
      * This method differs from [[render()]] in that it does not apply any layout.
      * @param string $view the view name. Please refer to [[render()]] on how to specify a view name.
      * @param array $params the parameters (name-value pairs) that should be made available in the view.
      * @return string the rendering result.
      * @throws InvalidParamException if the view file does not exist.
      */

     public function renderPartial($view, $params = [])
     {
         return $this->getView()->render($view, $params, $this);
     }

     /**
      * Renders a view file.
      * 这个就是用来渲染一个文件,$file为文件实路径或别名路径
      * @param string $file the view file to be rendered. This can be either a file path or a path alias.
      * @param array $params the parameters (name-value pairs) that should be made available in the view.
      * @return string the rendering result.
      * @throws InvalidParamException if the view file does not exist.
      */

     public function renderFile($file, $params = [])
     {
         return $this->getView()->renderFile($file, $params, $this);
     }

     /**
      * Returns the view object that can be used to render views or view files.
      * 获取view组件 应该是getter方法
      * [[render()]], [[renderPartial()]] and [[renderFile()]] 方法调用这个对象实现视图的渲染
      * If not set, it will default to the "view" application component.
      * @return View|\yii\web\View the view object that can be used to render views or view files.
      */
     public function getView()
     {
         if ($this->_view === null) {
             $this->_view = Yii::$app->getView();
         }
         return $this->_view;
     }

     /**
      * Sets the view object to be used by this controller.
      * 设置view对象,这个应该是setter方法
      * @param View|\yii\web\View $view the view object that can be used to render views or view files.
      */
     public function setView($view)
     {
         $this->_view = $view;
     }

     /**
      * Returns the directory containing view files for this controller.
      * 获取这个控制器对应的view的文件路径
      * 默认返回的是模块下的视图路径
      * @return string the directory containing the view files for this controller.
      */
     public function getViewPath()
     {
         if ($this->_viewPath === null) {
             $this->_viewPath = $this->module->getViewPath() . DIRECTORY_SEPARATOR . $this->id;
         }
         return $this->_viewPath;
     }

     /**
      * Sets the directory that contains the view files.
      * 设置这个控制器对应的view文件的路径 setter方法
      * @param string $path the root directory of view files.
      * @throws InvalidParamException if the directory is invalid
      * @since 2.0.7
      */
     public function setViewPath($path)
     {
         $this->_viewPath = Yii::getAlias($path);
     }

     /**
      * Finds the applicable layout file.
      * 查找布局文件
      * @param View $view the view object to render the layout file.
      * @return string|boolean the layout file path, or false if layout is not needed.
      * Please refer to [[render()]] on how to specify this parameter.
      * @throws InvalidParamException if an invalid path alias is used to specify the layout.
      */
     public function findLayoutFile($view)
     {
         $module = $this->module;
         if (is_string($this->layout)) {
             //如果当前控制器设置了布局文件,则直接使用所设置的布局文件
             $layout = $this->layout;
         } elseif ($this->layout === null) {
             //如果没有设置布局文件,则查找所有的父模块的布局文件。
             while ($module !== null && $module->layout === null) {
                 $module = $module->module;
             }
             if ($module !== null && is_string($module->layout)) {
                 $layout = $module->layout;
             }
         }
         //如果没有设置布局文件,返回false
         if (!isset($layout)) {
             return false;
         }
         /*
          * 布局文件有三种路径写法
          * 1、别名,以“@”开头,这种会在别名路径中查找布局文件
          * 2、应用下的布局文件,以“/”开头,这个会从应用程序的布局文件目录下面查找布局文件
          * 3、其它情况,   这个会从当前模块的布局文件目录下查查找布局文件
          */
         if (strncmp($layout, '@', 1) === 0) {
             $file = Yii::getAlias($layout);
         } elseif (strncmp($layout, '/', 1) === 0) {
             $file = Yii::$app->getLayoutPath() . DIRECTORY_SEPARATOR . substr($layout, 1);
         } else {
             $file = $module->getLayoutPath() . DIRECTORY_SEPARATOR . $layout;
         }
         //如果布局文件有文件扩展名,返回
         if (pathinfo($file, PATHINFO_EXTENSION) !== '') {
             return $file;
         }
         //加上默认的文件扩展名。
         $path = $file . '.' . $view->defaultExtension;
         //如果文件不存在,并且,默认的文件扩展名也不是php,则加上.php作为扩展名。
         if ($view->defaultExtension !== 'php' && !is_file($path)) {
             $path = $file . '.php';
         }

         return $path;
     }

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

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

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

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

    Widget类中开始,获取视图对象,获取widget ID,渲染视图,获取路径方法注释: private $_id; /** * Returns the ID of the widget. * 返回插 ...

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

    Yii2请求处理流程: 首先:项目路径/web/index.php (new yii\web\Application($config))->run();//根据配置文件创建App实例,先实例化y ...

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

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

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

    接着上次的继续阅读BaseYii.php vendor/yiisoft/yii2/BaseYii.php—— public static function getRootAlias($alias)// ...

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

    接下来阅读BaseYii.php vendor/yiisoft/yii2/BaseYii.php—— namespace yii; use yii\base\InvalidConfigExceptio ...

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

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

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

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

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

    ServiceLocator,服务定位类,用于yii2中的依赖注入,通过以ID为索引的方式缓存服务或则组件的实例来定位服务或者组件: namespace yii\di; use Yii; use Cl ...

随机推荐

  1. 菜鸟学Linux命令:find命令 查找文件

    find命令是Linux下最常用的命令之一,灵活的使用find命令,你会发现查找文件变得十分简单. 命令格式 find [指定查找目录]  [查找规则(选项)]  [查找完后执行的动作] 参数规则 - ...

  2. .net学习之类与对象、new关键字、构造函数、常量和只读变量、枚举、结构、垃圾回收、静态成员、静态类等

    1.类与对象的关系类是对一类事务的统称,是抽象的,不能拿来直接使用,比如汽车,没有具体指哪一辆汽车对象是一个具体存在的,看的见,摸得着的,可以拿来直接使用,比如我家的那辆刚刚买的新汽车,就是具体的对象 ...

  3. Java Hour 46 SLF4J

    SLF4J(Simple Logging Facade for Jave). 上个章节在配置启动项目后,会报一个奇怪的错误,一开始以为是Maven 依赖有冲突,其实是和slf4j 密切相关的. 本文原 ...

  4. Start GitHub

    google 的各种被墙大家都懂的,所以转向GitHub 的怀抱. 以前其实也关注过,这回是下决心好好试用了. Fork repositories Forking creates a new, uni ...

  5. hdu 4296 贪心

    证明转自:  here 题意:有 n 个地板,每个地板 i 有两个权值 Wi, Si,且 PDV(i) =  (ΣWj) - Si ( j 表示在 i 上面的地板).问如何调整顺序,使得[max(PD ...

  6. SQLite使用方法 SQLiteOpenHelper操作(转)

    SQLiteOpenHelper主要用于 创建数据库 SQLiteDatabase 主要用于 执行sql语句 程序内使用SQLite数据库是通过SQLiteOpenHelper进行操作 1.      ...

  7. 修改ViewPager调用setCurrentItem时,滑屏的速度

    原文摘自: 修改ViewPager调用setCurrentItem时,滑屏的速度 在使用ViewPager的过程中,有需要直接跳转到某一个页面的情况,这个时候就需要用到ViewPager的setCur ...

  8. Android 一个app启动另一个app

    最近,一个app启动另一个app,这个玩法挺火的嘛,有没有试过更新QQ到5.1版本,QQ的健康里面就可以添加其他app,实现从QQ跳转到其他app应用.这个挺好玩的,一下子带来了多少流量啊. 一.先来 ...

  9. ContentProvider往通讯录添加联系人和获取联系人

    public class MainActivity extends Activity { private People people; private List<People> pList ...

  10. CodeForces 300C 最短路

    A Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Pr ...