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 ...
随机推荐
- QuickSort 快速排序 基于伪代码实现
本文原创,转载请注明地址 http://www.cnblogs.com/baokang/p/4737492.html 伪代码 quicksort(A, lo, hi) if lo < hi p ...
- delphi xe4 程序添加管理员权限要求后不能调试的解决方法
环境: win7 企业版 xe4 问题: 把项目设置为需要管理员权限才能运行后,调试会弹出一个提示框,如图:
- SQL中rowcount与@@rowcount
rowcount的用法: rowcount的作用就是用来限定后面的sql在返回指定的行数之后便停止处理,比如下面的示例, select * from 表A 这样的查询只会返回表A中的前10条数据.它和 ...
- 移动端web开发技巧
META相关 1. 添加到主屏后的标题(IOS)
- 登录oracle数据库提示账户锁定解决方法
问题再现: 由于更改了oracle账户的密码,退出重新连接oracle出现了账户被锁定的情况. 请了百度君出来卸载一下,问题已解决. 在cmd下:sqlplus /nolog 然后:以dba身份登录: ...
- 【转】XAMPP中配置多个网站
XAMPP虚拟主机配置,多域名绑定访问本地站点 XAMPP有时候你需要一些顶级域名访问方式来访问你本地的项目也就是虚拟主机配置,这时候就需要配置虚拟主机,给你的目录绑定一个域名,实现多域名绑定访问. ...
- 在android studio 中使用applicationid的问题
现在我需要对项目app的某个功能做性能测试,主要测试耗电量的多少. 1.我想到的方式是,我需要在同一台手机测试,同一个应用,需要安装在手机两次,第二次安装不覆盖第一次的安装. 在android stu ...
- Ubuntu 用vsftpd 配置FTP服务器
网上的文章好难懂啊..只想要简单粗暴,弄好能用就行啊,复杂的以后研究不行吗...折腾好久,其实弄出来能用不就这么点内容吗... 本文在Ubuntu Server 14.04 amd64系统测试. Ma ...
- ACM: NBUT 1105 多连块拼图 - 水题 - 模拟
NBUT 1105 多连块拼图 Time Limit:1000MS Memory Limit:65535KB 64bit IO Format: Practice Appoint ...
- Android入门(三):使用TextView、EditText 和Button接口组件
我使用的IDE是Android Studio 2.1,虽然使用Eclipse也可以进行Android的开发,但是网上的大神大都推荐Android Studio,愿意了解的朋友可以参考知乎上关于Andr ...