这几天有点忙今天好些了,继续上次的module来吧

     /**
* Returns the directory that contains the controller classes according to [[controllerNamespace]].
*根据控制器的命名空间返回控制器的目录路径
* Note that in order for this method to return a value, you must define
* an alias for the root namespace of [[controllerNamespace]].
* 为了使该方法返回正确的值,必须为[[controllerNamespace]]定义一个根别名
* @return string the directory that contains the controller classes.
* @throws InvalidParamException if there is no alias defined for the root namespace of [[controllerNamespace]].
*/
public function getControllerPath()
{
//通过将命名空间转换为路径构造别名路径,然后通过getAlias方法取得控制器的绝对路径
return Yii::getAlias('@' . str_replace('\\', '/', $this->controllerNamespace));
} /**
* Returns the directory that contains the view files for this module.
* 取得当前模块的视图文件目录路径
* @return string the root directory of view files. Defaults to "[[basePath]]/views".
*/
public function getViewPath()
{
if ($this->_viewPath !== null) {
return $this->_viewPath;
} else {
//getBasePath()返回当前模块的根路径,然后拼接出视图文件路径
return $this->_viewPath = $this->getBasePath() . DIRECTORY_SEPARATOR . 'views';
}
} /**
* Sets the directory that contains the view files.设置视图文件目录路径
* @param string $path the root directory of view files.
* @throws InvalidParamException if the directory is invalid
*/
public function setViewPath($path)
{
$this->_viewPath = Yii::getAlias($path);
} /**
* Returns the directory that contains layout view files for this module.
* 取得当前模块的布局文件路径
* @return string the root directory of layout files. Defaults to "[[viewPath]]/layouts".
*/
public function getLayoutPath()
{
if ($this->_layoutPath !== null) {
return $this->_layoutPath;
} else {//getBasePath()返回当前模块的根路径,然后拼接出布局文件目录路径
return $this->_layoutPath = $this->getViewPath() . DIRECTORY_SEPARATOR . 'layouts';
}
} /**
* Sets the directory that contains the layout files.设置当前模块的布局文件路径
* @param string $path the root directory or path alias of layout files.
* @throws InvalidParamException if the directory is invalid
*/
public function setLayoutPath($path)
{
$this->_layoutPath = Yii::getAlias($path);
} /**
* Defines path aliases. 定义路径别名
* This method calls [[Yii::setAlias()]] to register the path aliases.
* This method is provided so that you can define path aliases when configuring a module.
* 通过调用[[Yii::setAlias()]]注册路径别名,方便在配置模块的时候定义路径别名
* @property array list of path aliases to be defined. The array keys are alias names
* (must start with '@') and the array values are the corresponding paths or aliases.
* See [[setAliases()]] for an example.
* @param array $aliases list of path aliases to be defined. The array keys are alias names
* (must start with '@') and the array values are the corresponding paths or aliases.
* For example,
*
* ~~~
* [
* '@models' => '@app/models', // an existing alias
* '@backend' => __DIR__ . '/../backend', // a directory
* ]
* ~~~
*/
public function setAliases($aliases)
{
foreach ($aliases as $name => $alias) {
Yii::setAlias($name, $alias);//调用[[Yii::setAlias()]]路径别名
}
} /**
* Checks whether the child module of the specified ID exists.
* This method supports checking the existence of both child and grand child modules.
* @param string $id module ID. For grand child modules, use ID path relative to this module (e.g. `admin/content`).
* @return boolean whether the named module exists. Both loaded and unloaded modules
* are considered.
*/
public function hasModule($id)
{
if (($pos = strpos($id, '/')) !== false) {
// sub-module 如果模块ID格式为 `admin/content` 取出当前模块的子模块
$module = $this->getModule(substr($id, , $pos));
//如果没有取到,返回false
return $module === null ? false : $module->hasModule(substr($id, $pos + ));
} else {
//模块ID没有父模块的情况,直接判断_modules数组中是否有值
return isset($this->_modules[$id]);
}
} /**
* Retrieves the child module of the specified ID.取出指定模块的子模块
* This method supports retrieving both child modules and grand child modules.
* 该方法支持检索子模块和子模块的子模块
* @param string $id module ID (case-sensitive). To retrieve grand child modules,
* use ID path relative to this module (e.g. `admin/content`).
* 检索子模块,使用标识路径相对
* @param boolean $load whether to load the module if it is not yet loaded. 是否加载模块
* @return Module|null the module instance, null if the module does not exist. 不存在删除
* @see hasModule()
*/
public function getModule($id, $load = true)
{
if (($pos = strpos($id, '/')) !== false) {
// sub-module 判断模块id格式是否为 `admin/content` 取子模块
$module = $this->getModule(substr($id, , $pos)); return $module === null ? null : $module->getModule(substr($id, $pos + ), $load);
} if (isset($this->_modules[$id])) {
if ($this->_modules[$id] instanceof Module) {
return $this->_modules[$id];//如果_modules数组中有该模块,直接返回该模块
} elseif ($load) {//否则,先实例化后返回
Yii::trace("Loading module: $id", __METHOD__);
/* @var $module Module */
$module = Yii::createObject($this->_modules[$id], [$id, $this]);
$module->setInstance($module);
return $this->_modules[$id] = $module;
}
}
//不存在,返回null
return null;
} /**
* Adds a sub-module to this module. 为当前模块添加子模块
* @param string $id module ID 模块标识
* @param Module|array|null $module the sub-module to be added to this module. This can
* be one of the followings:
*
* - a [[Module]] object
* - a configuration array: when [[getModule()]] is called initially, the array
* will be used to instantiate the sub-module
* - null: the named sub-module will be removed from this module
*/
public function setModule($id, $module)
{
if ($module === null) {
//为空删除
unset($this->_modules[$id]);
} else {//存在则添加
$this->_modules[$id] = $module;
}
}

yii2源码学习笔记(十五)的更多相关文章

  1. yii2源码学习笔记(十四)

    Module类是模块和应用类的基类. yiisoft\yii2\base\Module.php <?php /** * @link http://www.yiiframework.com/ * ...

  2. yii2源码学习笔记(十九)

    view剩余代码 /** * @return string|boolean the view file currently being rendered. False if no view file ...

  3. yii2源码学习笔记(十二)

    继续了解controller基类. /** * Runs a request specified in terms of a route.在路径中指定的请求. * The route can be e ...

  4. yii2源码学习笔记(十)

    继续了解Application. /** * Registers the errorHandler component as a PHP error handler. * 注册errorHandler ...

  5. yii2源码学习笔记(十六)

    Module类的最后代码 /** * Registers sub-modules in the current module. * 注册子模块到当前模块 * Each sub-module shoul ...

  6. async-validator 源码学习笔记(五):Schema

    系列文章: 1.async-validator 源码学习(一):文档翻译 2.async-validator 源码学习笔记(二):目录结构 3.async-validator 源码学习笔记(三):ru ...

  7. yii2源码学习笔记(九)

    Application是所有应用程序类的基类,接下来了解一下它的源码.yii2\base\Application.php. <?php /** * @link http://www.yiifra ...

  8. yii2源码学习笔记(八)

    Action是所有控制器的基类,接下来了解一下它的源码.yii2\base\Action.php <?php /** * @link http://www.yiiframework.com/ * ...

  9. 老刘 Yii2 源码学习笔记之 Action 类

    Action 的概述 InlineAction 就是内联动作,所谓的内联动作就是放到controller 里面的 actionXXX 这种 Action.customAction 就是独立动作,就是直 ...

随机推荐

  1. yii中上传图片及文件

    Yii 提供了 CUploadedFile 来上传文件,比如图片,或者文档. 官方关于这个类的介绍 : http://www.yiichina.com/api/CUploadedFile CUploa ...

  2. nyoj 1100 WAJUEJI which home strong!

    WAJUEJI which home strong! 时间限制:1000 ms  |  内存限制:65535 KB 难度:2   描述 在一个山沟里,姐弟俩同时考上了大学.但由于家里拮据,所以这并不是 ...

  3. 8-7-Exercise

    链接:第二次小练 这次是我们这组出的题目~我出了一道......B-Prison rearrangement,感觉有点复杂~不过其实题目想通了还是很简单的...... @荆红浅醉出的是A.C.D,@从 ...

  4. mahout算法源码分析之Collaborative Filtering with ALS-WR (四)评价和推荐

    Mahout版本:0.7,hadoop版本:1.0.4,jdk:1.7.0_25 64bit. 首先来总结一下 mahout算法源码分析之Collaborative Filtering with AL ...

  5. mysql left( right ) join 使用on 与where的差异

    之前一直很困扰为什么left join要么一查没有数据,要么出现很多条重复数据. 百思不得其解,后来在网上到处找资料,总算明白了. 一定要理解下面几点:  (1)主表条件在on后面时附表只取满足主表筛 ...

  6. (6/18)重学Standford_iOS7开发_控制器多态性、导航控制器、选项卡栏控制器_课程笔记

    终于有时间跟新了,两周时间复(yu)习(xi)了5门考试累觉不爱...... ------------------------------------------------------------- ...

  7. 空格&amp;nbsp在不同浏览器中显示距离不一致问题解决方法

      在ie.firefox.chrome浏览器上显示的效果不太一样,主要是前面的空格宽度不同. 网上资料说不同的浏览器会有不同的默认字体.一般 IE默认字体都是宋体,而firefox和chrome的默 ...

  8. 基于 CoreText 实现的高性能 UITableView

    引起UITableView卡顿比较常见的原因有cell的层级过多.cell中有触发离屏渲染的代码(譬如:cornerRadius.maskToBounds 同时使用).像素是否对齐.是否使用UITab ...

  9. CocoaPods导入第三方库头文件自动补齐

    使用了一段时间CocoaPods来管理Objective-c的类库,方便了不少.但是有一个小问题,当我在xcode输入import关键字的时候,没有自动联想补齐代码的功能,需要手工敲全了文件名,难以适 ...

  10. oracle常用数据类型

    oracle中常用数据类型分为三大类: