码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2016-05-18 01:37:59      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:

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

  1    private $_id;
  2 
  3     /**
  4      * Returns the ID of the widget.
  5      * 返回插件的ID
  6      * @param boolean $autoGenerate whether to generate an ID if it is not set previously
  7      * @return string ID of the widget.
  8      */
  9     public function getId($autoGenerate = true)
 10     {
 11         if ($autoGenerate && $this->_id === null) {//如果ID为空,并且设置为允许自动生成ID,则自动生成ID
 12             $this->_id = static::$autoIdPrefix . static::$counter++;
 13         }
 14 
 15         return $this->_id;//返回widget ID
 16     }
 17 
 18     /**
 19      * Sets the ID of the widget.
 20      * 设置小部件ID
 21      * @param string $value id of the widget.
 22      */
 23     public function setId($value)
 24     {
 25         $this->_id = $value;//将小部件ID的值设置为$value
 26     }
 27 
 28     private $_view;
 29 
 30     /**
 31      * Returns the view object that can be used to render views or view files.
 32      * 返回视图对象
 33      * The [[render()]] and [[renderFile()]] methods will use
 34      * this view object to implement the actual view rendering.
 35      * If not set, it will default to the "view" application component.
 36      * @return \yii\web\View the view object that can be used to render views or view files.
 37      */
 38     public function getView()
 39     {
 40         if ($this->_view === null) {
 41             $this->_view = Yii::$app->getView();//如果视图对象为空,调用getView()方法取得视图对象实例
 42         }
 43 
 44         return $this->_view;
 45     }
 46 
 47     /**
 48      * Sets the view object to be used by this widget.
 49      * 设置当前小部件调用的视图对象实例
 50      * @param View $view the view object that can be used to render views or view files.
 51      */
 52     public function setView($view)
 53     {
 54         $this->_view = $view;//将传入的$view赋值给_view
 55     }
 56 
 57     /**
 58      * Executes the widget.
 59      * 执行小部件--暂时不清楚该方法在哪里实现
 60      * @return string the result of widget execution to be outputted.
 61      */
 62     public function run()
 63     {
 64     }
 65 
 66     /**
 67      * Renders a view.
 68       * 渲染一个视图 --该方法实际调用的是View类中的同名方法
 69      * The view to be rendered can be specified in one of the following formats:
 70      * 被渲染的视图可以用下列方式指定
 71      *
 72      * - path alias (e.g. "@app/views/site/index");
 73      *   路径别名
 74      * - absolute path within application (e.g. "//site/index"): the view name starts with double slashes.
 75      *   The actual view file will be looked for under the [[Application::viewPath|view path]] of the application.
 76      *   绝对路径,将会在[Application::viewPath|view path]下查找文件
 77      * - absolute path within current module (e.g. "/site/index"): the view name starts with a single slash.
 78      *   The actual view file will be looked for under the [[Module::viewPath|view path]] of the [[Controller::module|current module]].
 79      *   模块下的绝对路径,将会在[Module::viewPath|view path]下查找文件
 80      * - relative view (e.g. "index"): the view name does not start with `@` or `/`. The corresponding view file will be
 81      *   looked for under the [[ViewContextInterface::getViewPath()|view path]] of the view `$context`.
 82      *   相对路径,将会在[ViewContextInterface::getViewPath()|view path]下查找文件
 83      * If the view name does not contain a file extension, it will use the default one `.php`.
 84      *
 85      * @param string $view the view name.
 86      * @param array $params the parameters (name-value pairs) that should be made available in the view.
 87      * @return string the rendering result.
 88      * @throws InvalidParamException if the view file does not exist.
 89      */
 90     public function render($view, $params = [])
 91     {
 92         return $this->getView()->render($view, $params, $this);//调用view类中的render方法渲染指定的视图
 93     }
 94 
 95     /**
 96      * Renders a view file.
 97      * 渲染一个视图文件 --该方法实际调用的是View类中的同名方法
 98      * @param string $file the view file to be rendered. This can be either a file path or a path alias.
 99      * @param array $params the parameters (name-value pairs) that should be made available in the view.
100      * @return string the rendering result.
101      * @throws InvalidParamException if the view file does not exist.
102      */
103     public function renderFile($file, $params = [])
104     {
105         return $this->getView()->renderFile($file, $params, $this);
106     }
107 
108     /**
109      * Returns the directory containing the view files for this widget.
110      * 返回小部件的视图文件路径
111      * The default implementation returns the ‘views‘ subdirectory under the directory containing the widget class file.
112      * 默认返回当前小部件文件所在的views子目录
113      * @return string the directory containing the view files for this widget.
114      */
115     public function getViewPath()
116     {
117         $class = new ReflectionClass($this);
118 
119         return dirname($class->getFileName()) . DIRECTORY_SEPARATOR . ‘views‘;//取得小部件类文件所在的目录,拼接为views目录
120     }

 

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

标签:

原文地址:http://www.cnblogs.com/isleep/p/5503756.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!