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

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目录
     }
上一篇:js 时间日期函数小结


下一篇:VTK序列图像的读取[转][改]