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. 安装配置LDAP遇到的问题

    问题1:安装完启动ldap服务报错: ldap: unrecognized service? 原因在于新版的openldap将服务名改为了slapd,使用service slapd start即可启动 ...

  2. tornado django flask 跨域解决办法(cors)

    XMLHttpRequest cannot load http://www.baidu.com. No 'Access-Control-Allow-Origin' header is present ...

  3. 序列化对象为xml字符串

    /// <summary>    /// 序列化对象为xml字符串    /// </summary>    /// <param name="obj" ...

  4. java导出生成word(类似简历导出)

    参考帖子: http://www.cnblogs.com/lcngu/p/5247179.html http://www.cnblogs.com/splvxh/archive/2013/03/15/2 ...

  5. Unity3D 更新文件下载器

    使用说明: 1)远端更新服务器目录 Package |----list.txt |----a.bundle |----b.bundle 2)list.txt是更新列表文件 格式是 a.bundle|r ...

  6. maven install 构建报错(2)

    错误:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin: 2.3 . 2 :compile ( default ...

  7. NYOJ题目20吝啬的国度

    -----------------------------------------n-1条边的无向连通图是一棵树,又因为树上两点之间的路径是唯一的,所以解是唯一的.(注意并不一定是二叉树,所以最好采用 ...

  8. Java基本数据类型与位运算

    >>赋值运算符 赋值使用操作符“=”.它的意思是“取右边的值(即右值),把它复制给左边(即左值)”.右值可以是任何 常数.变量或者表达式 (只要它能 生成 一个值就行).但左值必须是一个明 ...

  9. Nginx 反代理其他搜索引擎

    反向代理 反向代理(Reverse Proxy)方式是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给internet上请求连接的客 ...

  10. Pyqt 国际化多语言支持

    国际化是指在代码设计上加入能方便的移植到其他国家和地区的特性, 给Pyqt 添加国际化支持需要五步 一.编写GUI.py 在要被翻译的text上用tr方法括起来 # -*- coding: utf-8 ...