Widget类中开始,获取视图对象,获取widget ID,渲染视图,获取路径方法注释:

    private $_id;

     /**
      * Returns the ID of the widget.
      * 返回插件的ID
      * @param boolean $autoGenerate whether to generate an ID if it is not set previously
      * @return string ID of the widget.
      */
     public function getId($autoGenerate = true)
     {
         if ($autoGenerate && $this->_id === null) {//如果ID为空,并且设置为允许自动生成ID,则自动生成ID
             $this->_id = static::$autoIdPrefix . static::$counter++;
         }

         return $this->_id;//返回widget ID
     }

     /**
      * Sets the ID of the widget.
      * 设置小部件ID
      * @param string $value id of the widget.
      */
     public function setId($value)
     {
         $this->_id = $value;//将小部件ID的值设置为$value
     }

     private $_view;

     /**
      * Returns the view object that can be used to render views or view files.
      * 返回视图对象
      * The [[render()]] and [[renderFile()]] methods will use
      * this view object to implement the actual view rendering.
      * If not set, it will default to the "view" application component.
      * @return \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();//如果视图对象为空,调用getView()方法取得视图对象实例
         }

         return $this->_view;
     }

     /**
      * Sets the view object to be used by this widget.
      * 设置当前小部件调用的视图对象实例
      * @param View $view the view object that can be used to render views or view files.
      */
     public function setView($view)
     {
         $this->_view = $view;//将传入的$view赋值给_view
     }

     /**
      * Executes the widget.
      * 执行小部件--暂时不清楚该方法在哪里实现
      * @return string the result of widget execution to be outputted.
      */
     public function run()
     {
     }

     /**
      * Renders a view.
       * 渲染一个视图 --该方法实际调用的是View类中的同名方法
      * The view to be rendered can be specified in one of the following formats:
      * 被渲染的视图可以用下列方式指定
      *
      * - path alias (e.g. "@app/views/site/index");
      *   路径别名
      * - absolute path within application (e.g. "//site/index"): the view name starts with double slashes.
      *   The actual view file will be looked for under the [[Application::viewPath|view path]] of the application.
      *   绝对路径,将会在[Application::viewPath|view path]下查找文件
      * - absolute path within current module (e.g. "/site/index"): the view name starts with a single slash.
      *   The actual view file will be looked for under the [[Module::viewPath|view path]] of the [[Controller::module|current module]].
      *   模块下的绝对路径,将会在[Module::viewPath|view path]下查找文件
      * - relative view (e.g. "index"): the view name does not start with `@` or `/`. The corresponding view file will be
      *   looked for under the [[ViewContextInterface::getViewPath()|view path]] of the view `$context`.
      *   相对路径,将会在[ViewContextInterface::getViewPath()|view path]下查找文件
      * If the view name does not contain a file extension, it will use the default one `.php`.
      *
      * @param string $view the 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 render($view, $params = [])
     {
         return $this->getView()->render($view, $params, $this);//调用view类中的render方法渲染指定的视图
     }

     /**
      * Renders a view file.
      * 渲染一个视图文件 --该方法实际调用的是View类中的同名方法
      * @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 directory containing the view files for this widget.
      * 返回小部件的视图文件路径
      * The default implementation returns the 'views' subdirectory under the directory containing the widget class file.
      * 默认返回当前小部件文件所在的views子目录
      * @return string the directory containing the view files for this widget.
      */
     public function getViewPath()
     {
         $class = new ReflectionClass($this);

         return dirname($class->getFileName()) . DIRECTORY_SEPARATOR . 'views';//取得小部件类文件所在的目录,拼接为views目录
     }

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

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

    controller类的render部分,用于渲染视图和布局文件: /** * Returns all ancestor modules of this controller. * 获取当前控制器所有 ...

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

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

  3. Werkzeug源码阅读笔记(三)

    这次主要讲下werkzeug中的Local. 源码在werkzeug/local.py Thread Local 在Python中,状态是保存在对象中.Thread Local是一种特殊的对象,它是对 ...

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

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. rabbitmq 简单梳理

    概念: 生产者(Producer,简写P),负责发布消息. “交换机”(Exchange, 简写X), 负责中转消息. 路由(Route, 简写R), 即 X->Q的路线名. 消息队列 (Que ...

  2. 彻底卸载MySQL数据库教程

    有时候MySQL不能完全卸载,这时候必须通过一些途径删除掉注册表和一些残余的文件,然后才能重新安装才可以成功! 1.控制面板-->所有控制面板项-->程序和功能,卸载mysql serve ...

  3. Eclipse下无法自动编译,或者WEB-INF/classes目录下没文件,编译失败的解决办法(转载)

    文章来源:http://www.cnblogs.com/xfiver/archive/2010/07/07/1772764.html 1.  IOException parsing XML docum ...

  4. python DBUtils.PooledDB 中 maxcached 和 maxconnections

    PooledDB 有这么几个参数 mincached : the initial number of idle connections in the pool (the default of 0 me ...

  5. bzoj4260

    题目大意:求不相交的两段区间,两段的异或和加起来最大是多少 区间异或和记得转化成前缀和啊我个sb 变成一对数的异或值就变成trie了啊 两段区间的话,从左往右一颗trie,从右往左一颗trie #in ...

  6. shell不能执行su 后的脚本

    问题:在shell脚本中执行“su – 用户名”后,脚本终止执行,并且切换到su 中指定用户名的交互式界面  现象:我在root中执行一个脚本,但是其中的一些命令或脚本必须用oracle用户来执行., ...

  7. 联合体(union)的使用方法及其本质

    转自:http://blog.csdn.net/huqinwei987/article/details/23597091 有些基础知识快淡忘了,所以有必要复习一遍,在不借助课本死知识的前提下做些推理判 ...

  8. Source Insight编辑器配置

    Sublime Text 无疑是一款很优秀的编辑器和阅读器,可惜对于中文编码不支持,网上的ConvertTOUTF8存在BUG,经常转码失败,体验很不好. 现在开始使用source insight,这 ...

  9. iOS第三方Api及常用框架总结

    iOS常用框架汇总: SVProgressHUD:产生覆盖层,禁止某种操作 SDWebImage: 专业下载图片框架 AFN:网络数据请求框架 MJExtension,模型对象之间互转 第三方分享第三 ...

  10. BurpSuite设置公共WIFI抓包

    1.电脑连接公共WIFI