yii2源码学习笔记(十六)
Module类的最后代码
/**
* Registers sub-modules in the current module.
* 注册子模块到当前模块
* Each sub-module should be specified as a name-value pair, where
* name refers to the ID of the module and value the module or a configuration
* array that can be used to create the module. In the latter case, [[Yii::createObject()]]
* will be used to create the module.
* 子模块以键值对的方式指定,键名为模块ID,键值为模块对象或者用于创建模块对象的配置数组
* If a new sub-module has the same ID as an existing one, the existing one will be overwritten silently.
* 如果标识相同 会覆盖
* The following is an example for registering two sub-modules:
*
* ~~~
* [
* 'comment' => [
* 'class' => 'app\modules\comment\CommentModule',
* 'db' => 'db',
* ],
* 'booking' => ['class' => 'app\modules\booking\BookingModule'],
* ]
* ~~~
*
* @param array $modules modules (id => module configuration or instances)
*/
public function setModules($modules)
{
foreach ($modules as $id => $module) {
$this->_modules[$id] = $module; //注册子模块,覆盖同名模块
}
} /**
* Runs a controller action specified by a route. 运行路由中指定的控制器方法
* This method parses the specified route and creates the corresponding child module(s), controller and action
* instances. It then calls [[Controller::runAction()]] to run the action with the given parameters.
* If the route is empty, the method will use [[defaultRoute]].
* 解析指定的路由,创建对应的子模块、控制器、方法实例,调用[[Controller::runAction()]]给定的参数运行控制器中的方法
* @param string $route the route that specifies the action. 指定行动的路线。
* @param array $params the parameters to be passed to the action 操作的参数
* @return mixed the result of the action. 操作结果
* @throws InvalidRouteException if the requested route cannot be resolved into an action successfully
*/
public function runAction($route, $params = [])
{
$parts = $this->createController($route); //根据路由创建控制器
if (is_array($parts)) {
/* @var $controller Controller */
list($controller, $actionID) = $parts; //获得$actionId和$controller
$oldController = Yii::$app->controller;
Yii::$app->controller = $controller;
$result = $controller->runAction($actionID, $params); //运行控制器 加载action方法
Yii::$app->controller = $oldController; return $result;
} else {
$id = $this->getUniqueId();
throw new InvalidRouteException('Unable to resolve the request "' . ($id === '' ? $route : $id . '/' . $route) . '".');
}
} /**
* Creates a controller instance based on the given route.
* 根据给定的路径创建一个控制器实例。
* The route should be relative to this module. The method implements the following algorithm
* to resolve the given route:
* 相对这个模块的路径。该方法实现了以下算法来解决给定的路径:
* 1. If the route is empty, use [[defaultRoute]]; 路径为空,调用默认的路径
* 2. If the first segment of the route is a valid module ID as declared in [[modules]],
* call the module's `createController()` with the rest part of the route;
* 3. If the first segment of the route is found in [[controllerMap]], create a controller
* based on the corresponding configuration found in [[controllerMap]];
* 4. The given route is in the format of `abc/def/xyz`. Try either `abc\DefController`
* or `abc\def\XyzController` class within the [[controllerNamespace|controller namespace]].
*
* If any of the above steps resolves into a controller, it is returned together with the rest
* part of the route which will be treated as the action ID. Otherwise, false will be returned.
*
* @param string $route the route consisting of module, controller and action IDs.
* 由模块、控制器和动作标识组成的路径。
* @return array|boolean If the controller is created successfully, it will be returned together
* with the requested action ID. Otherwise false will be returned.
* 如果控制器成功创建,将与被请求的操作标识一起返回,否则将返回false。
* @throws InvalidConfigException if the controller class and its file do not match.
* 如果控制器类及其文件不匹配,抛出异常
*/
public function createController($route)
{
if ($route === '') {//路径为空,调用默认的路径
$route = $this->defaultRoute;
} // double slashes or leading/ending slashes may cause substr problem
$route = trim($route, '/'); //去掉两边的反斜线,
if (strpos($route, '//') !== false) {
return false; //如果路径中包含双斜线,返回false
} if (strpos($route, '/') !== false) {
list ($id, $route) = explode('/', $route, );//将路径按反斜线分割为两个元素的数组,
} else {
$id = $route;
$route = '';
} // module and controller map take precedence 优先判断模块和控制器映射
if (isset($this->controllerMap[$id])) {
//如果$id是控制器ID,实例化控制器,返回控制器实例和后面的路径$route
$controller = Yii::createObject($this->controllerMap[$id], [$id, $this]);
return [$controller, $route];
}
$module = $this->getModule($id);
if ($module !== null) {//如果$id是模块ID,实例化控制器,返回控制器实例和后面的路径$route
return $module->createController($route);
} if (($pos = strrpos($route, '/')) !== false) {
//上面两种情况都不是,则表示还有子模块,构造子模块的标识
$id .= '/' . substr($route, , $pos);
$route = substr($route, $pos + );
} $controller = $this->createControllerByID($id);
if ($controller === null && $route !== '') {//实例化控制器 组装控制器实例和后面的路径$route
$controller = $this->createControllerByID($id . '/' . $route);
$route = '';
}
//存在返回控制器实例和后面的路径$route,否则返回false
return $controller === null ? false : [$controller, $route];
} /**
* Creates a controller based on the given controller ID.
* 根据给定的控制器标识创建控制器
* The controller ID is relative to this module. The controller class
* should be namespaced under [[controllerNamespace]].
* 当前模块的控制器标识,控制器类应在 [[controllerNamespace]]的命名空间下
* Note that this method does not check [[modules]] or [[controllerMap]].
*
* @param string $id the controller ID 控制器标识
* @return Controller the newly created controller instance, or null if the controller ID is invalid.
* 新创建的控制器实例,为null则控制器标识无效
* @throws InvalidConfigException if the controller class and its file name do not match.
* This exception is only thrown when in debug mode.
*/
public function createControllerByID($id)
{
$pos = strrpos($id, '/');
if ($pos === false) {
$prefix = ''; //是否包含反斜线,
$className = $id;
} else {//将路径按反斜线分割为两个元素
$prefix = substr($id, , $pos + );
$className = substr($id, $pos + );
} if (!preg_match('%^[a-z][a-z0-9\\-_]*$%', $className)) {
return null;//正则判断是否符合规则
}
if ($prefix !== '' && !preg_match('%^[a-z0-9_/]+$%i', $prefix)) {
return null;
}
//组装控制器名
$className = str_replace(' ', '', ucwords(str_replace('-', ' ', $className))) . 'Controller';
$className = ltrim($this->controllerNamespace . '\\' . str_replace('/', '\\', $prefix) . $className, '\\');
if (strpos($className, '-') !== false || !class_exists($className)) {
return null; //控制器名有 ‘-’或不存在则为null
} if (is_subclass_of($className, 'yii\base\Controller')) {//检查对象是否有父类或子类
return Yii::createObject($className, [$id, $this]); //创建控制器
} elseif (YII_DEBUG) {
throw new InvalidConfigException("Controller class must extend from \\yii\\base\\Controller.");
} else {
return null;
}
} /**
* This method is invoked right before an action within this module is executed.
* 当前模块的Action执行前调用的方法,将触发[[EVENT_BEFORE_ACTION]]事件
* The method will trigger the [[EVENT_BEFORE_ACTION]] event. The return value of the method
* will determine whether the action should continue to run.
* 如果返回true,Action方法才会执行
* If you override this method, your code should look like the following:
*
* ```php
* public function beforeAction($action)
* {
* if (parent::beforeAction($action)) {
* // your custom code here
* return true; // or false if needed
* } else {
* return false;
* }
* }
* ```
*
* @param Action $action the action to be executed. 要执行的操作
* @return boolean whether the action should continue to be executed. 是否执行操作
*/
public function beforeAction($action)
{
$event = new ActionEvent($action);
$this->trigger(self::EVENT_BEFORE_ACTION, $event);//触发beforeAction事件
return $event->isValid;
} /**
* This method is invoked right after an action within this module is executed.
* 当前模块的Action执行后调用的方法,触发[[EVENT_AFTER_ACTION]]事件
* The method will trigger the [[EVENT_AFTER_ACTION]] event. The return value of the method
* will be used as the action return value.
* 如果返回true,后面的代码才会继续执行
* If you override this method, your code should look like the following:
*
* ```php
* public function afterAction($action, $result)
* {
* $result = parent::afterAction($action, $result);
* // your custom code here
* return $result;
* }
* ```
*
* @param Action $action the action just executed. 执行的操作
* @param mixed $result the action return result. 执行结果
* @return mixed the processed action result. 处理结果
*/
public function afterAction($action, $result)
{
$event = new ActionEvent($action);
$event->result = $result;
$this->trigger(self::EVENT_AFTER_ACTION, $event);//触发beforeAction事件
return $event->result;
}
}
yii2源码学习笔记(十六)的更多相关文章
- yii2源码学习笔记(十四)
Module类是模块和应用类的基类. yiisoft\yii2\base\Module.php <?php /** * @link http://www.yiiframework.com/ * ...
- yii2源码学习笔记(十九)
view剩余代码 /** * @return string|boolean the view file currently being rendered. False if no view file ...
- yii2源码学习笔记(十二)
继续了解controller基类. /** * Runs a request specified in terms of a route.在路径中指定的请求. * The route can be e ...
- yii2源码学习笔记(十)
继续了解Application. /** * Registers the errorHandler component as a PHP error handler. * 注册errorHandler ...
- yii2源码学习笔记(十五)
这几天有点忙今天好些了,继续上次的module来吧 /** * Returns the directory that contains the controller classes according ...
- async-validator 源码学习笔记(六):validate 方法
系列文章: 1.async-validator 源码学习(一):文档翻译 2.async-validator 源码学习笔记(二):目录结构 3.async-validator 源码学习笔记(三):ru ...
- yii2源码学习笔记(九)
Application是所有应用程序类的基类,接下来了解一下它的源码.yii2\base\Application.php. <?php /** * @link http://www.yiifra ...
- yii2源码学习笔记(八)
Action是所有控制器的基类,接下来了解一下它的源码.yii2\base\Action.php <?php /** * @link http://www.yiiframework.com/ * ...
- 老刘 Yii2 源码学习笔记之 Action 类
Action 的概述 InlineAction 就是内联动作,所谓的内联动作就是放到controller 里面的 actionXXX 这种 Action.customAction 就是独立动作,就是直 ...
随机推荐
- MongDB主从复制、复制集
主从复制比较简单,指定master.slave即可,其中master可写可读.slave只能读不能写.向master插入数据时,mongodb会自动将数据复制到slave节点.这样做的好处是读写分离, ...
- JSP实现数据库(MySQL)查询——Java Web练习(二)
1.创建数据库表student(数据库test01) 2.修改success.jsp页面,修改后的页面整体代码如下: <%@ page language="java" imp ...
- Android基础知识回顾
- oppo X907刷机包 COLOROS 1.0 正式版公布 安卓4.2.2
ROM介绍 本版本号将是X907史上最好的一版本号 全新COLOROS的UI 更新全局手势板操作 优化高速启动应用 安全保障中心也是一直採用COLOROS组成的 COLOROS 1.0给用户带来在线音 ...
- 软件项目量化管理(CMMI高成熟度)实践经验谈——之项目管理过程策划篇
续:软件项目量化管理(CMMI高成熟度)实践经验谈--之概述篇 二.项目管理过程 软件开发项目管理过程,从项目全视角来看,分为售前.售中.售后等三个大的阶段.本文所谈的是售中阶段项目管理过程,在售中阶 ...
- C语言实现双向链表删除节点、插入节点、双向输出等操作
#include<cstdio> #include<cstdlib> typedef struct DoubleLinkedList { int data; struct Do ...
- oracle数据库使用之数据查询入门
1.在查询过程中使用算术表达式对数据进行运算 student表结构如下: 最后一项salary表示每个人的月薪,我现在想查询每个人的年薪: 2.使用nvl函数处理null值,向表中插入一条数据,该数据 ...
- Android源码编译过程之九鼎开发板
build_kernel() { # 进入源码顶层目录 cd ${BS_DIR_KERNEL} || # 编译配置文件 make ${BS_CONFIG_KERNEL} ARCH=arm CROSS_ ...
- 使用html5兼容低版本浏览器
因为html5 新出的一些语义化的标签,在低版本浏览器下不能识别,举个例子,比如你写了一个 header 标签中,写了一段文本,在低版本浏览器下,肯定是能看到的,但是,那是他是不认识 header标签 ...
- css hack方法总结
IE6识别:下划线 _ 和 星号 *IE7识别:星号 *IE6-IE10识别: \9 css hack方式,条件注释法<!--[if IE]>这段文字只在IE浏览器显示<![endi ...