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目录
}
Yii源码阅读笔记(三十一)的更多相关文章
- Yii源码阅读笔记(十一)
controller类的render部分,用于渲染视图和布局文件: /** * Returns all ancestor modules of this controller. * 获取当前控制器所有 ...
- Yii源码阅读笔记(一)
今天开始阅读yii2的源码,想深入了解一下yii框架的工作原理,同时学习一下优秀的编码规范和风格.在此记录一下阅读中的小心得. 每个框架都有一个入口文件,首先从入口文件开始,yii2的入口文件位于we ...
- Werkzeug源码阅读笔记(三)
这次主要讲下werkzeug中的Local. 源码在werkzeug/local.py Thread Local 在Python中,状态是保存在对象中.Thread Local是一种特殊的对象,它是对 ...
- Yii源码阅读笔记(三)
接着上次的继续阅读BaseYii.php vendor/yiisoft/yii2/BaseYii.php—— public static function getRootAlias($alias)// ...
- Yii源码阅读笔记(三十五)
Container,用于动态地创建.注入依赖单元,映射依赖关系等功能,减少了许多代码量,降低代码耦合程度,提高项目的可维护性. namespace yii\di; use ReflectionClas ...
- Yii源码阅读笔记(三十四)
Instance类, 表示依赖注入容器或服务定位器中对某一个对象的引用 namespace yii\di; use Yii; use yii\base\InvalidConfigException; ...
- Yii源码阅读笔记(三十三)
ServiceLocator,服务定位类,用于yii2中的依赖注入,通过以ID为索引的方式缓存服务或则组件的实例来定位服务或者组件: namespace yii\di; use Yii; use Cl ...
- Yii源码阅读笔记(三十二)
web/Application类的注释,继承base/Application类,针对web应用的一些处理: namespace yii\web; use Yii; use yii\base\Inval ...
- Yii源码阅读笔记(三十)
Widget类是所有小部件的基类,开始,结束和渲染小部件内容的方法的注释: namespace yii\base; use Yii; use ReflectionClass; /** * Widget ...
随机推荐
- SpringMVC中遇到的Http400 Bad Request 总结
在搭建SpringMVC环境,在使用中遇到了多次Bad Request的连接,下面来总结下. 1.参数类型不对,如后台实体类的属性为int,但传来的参数为字符串 2.因为我的粗心,本来是要通过Ajax ...
- JavaScript之bind,call,apply
参考: http://rangercyh.blog.51cto.com/1444712/1615809 function foo(a,b) { this.x = this.x + a + b; } / ...
- How to install OpenBazaar Server in CentOS7
helps from: https://github.com/OpenBazaar/OpenBazaar-Server http://stackoverflow.com/questions/24917 ...
- spring jpa 实体互相引用返回restful数据循环引用报错的问题
spring jpa 实体互相引用返回restful数据循环引用报错的问题 Java实体里两个对象有关联关系,互相引用,比如,在一对多的关联关系里 Problem对象,引用了标签列表ProblemLa ...
- C Primer Plus_第6章_循环_编程练习
1.题略 #include int main(void) { int i; char ch[26]; for (i = 97; i <= (97+25); i++) { ch[i-97] = i ...
- ****CI框架源码阅读笔记7 配置管理组件 Config.php
http://blog.csdn.net/ohmygirl/article/details/41041597 一个灵活可控的应用程序中,必然会存在大量的可控参数(我们称为配置),例如在CI的主配置文件 ...
- MongoDB游标操作(4)
游标是什么? 通俗的说,游标不是查询结果,而是查询的返回资源,或者接口. 通过这个接口,你可以逐条读取. 声明游标: var cursor = db.collectioName.find(query ...
- Android之EACCES (Permission denied)与Permission denied异常探密
话说,Accipiter君,最近又开始怒学Android了,记得刚开始还是09年学的,现在的手机还是华为出的最早的一款Android手机C8500,那时候就想好好学习Android,赚点小钱,可是~~ ...
- 【Java EE 学习 17 上】【dbutils和回调函数】
一.dbutils的核心就是回调函数,可以说如果没有回调函数的思想,dbutils是不可能被开发出来的. 对于dbutils中的QuryRunner类,向该类的query方法提供不同的参数,可以得到不 ...
- oracle 12c中的隐含列
Invisible Columns 使用select * from ,desc 等看不到该列, DROP TABLE tab1 PURGE; CREATE TABLE tab1 ( id NUMB ...