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. hibernate中数据库方言

    在配置hibernate.cfg.xml时需指定使用数据库的方言: 例: <property name="dialect">org.hibernate.dialect. ...

  2. linux c 笔记-4 工程项目阅读推荐

    作者:周子涵链接:https://www.zhihu.com/question/27705862/answer/37738315来源:知乎著作权归作者所有,转载请联系作者获得授权. 转自网上不知道什么 ...

  3. iOS整体框架类图

    Cocoa是OS X和iOS操作系统的程序的运行环境. iOS的操作系统层次结构分为四层:触摸UI层,媒体层,核心服务层,核心OS层.其中底层框架提供iOS的基本服务和技术,高层次框架简历在低层次框架 ...

  4. 【转】Caffe初试(四)数据层及参数

    要运行caffe,需要先创建一个模型(model),如比较常用的Lenet,Alex等,而一个模型由多个层(layer)构成,每一层又由许多参数组成.所有的参数都定义在caffe.proto这个文件中 ...

  5. 关于网络-get/post

    关于网络: //英译  connection:连接 append:添加 resign:放弃 // 加载网页数据 步骤 [self.webView loadRequest:request]; NSURL ...

  6. quartz.net插件类库封装(含源码)

    1.前言 目录: 1.quartz.net任务调度:源码及使用文档 2.quartz.net插件类库封装 最近项目需要做一写任务作业调度的工作,最终选择了quartz.net这个插件,它提供了巨大的灵 ...

  7. Maven 添加自定义 archetype

    环境:jdk7 maven3.23 eclipse mars 关于maven是使用插件自带的还是单独下载的,应该都是可以的,但是要注意maven和jdk版本的问题,比如3.2.3版本的maven支持j ...

  8. .NET Remoting 体系结构 之 在 ASP.NET 中驻留远程服务器

    迄今为止,所有服务器示例都是运行在自驻留(self-hosted)的.NET 服务器上.自驻留的服务器必 须手动启动..NET Remoting 服务器也可以在许多其他的应用程序类型中启动.在 Win ...

  9. poj 3069 Saruman's Army

    Saruman's Army Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8477   Accepted: 4317 De ...

  10. python 函数传递方式

    在python中方法传递的参数到底是值传递还是引用传递? 1. 首先需要知道python中变量的类型:Python的变量分为可变变量和不可变变量. 针对于不可变的类型比如string int def ...