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 就是独立动作,就是直 ...
随机推荐
- Yii框架tips
db组件 'schemaCachingDuration'=>3600, 为什么不起做用?需要开缓存 如何在页面下边显示sql的查询时间在log组件的routes中加入 array('class' ...
- String类、 StringBuffer、基本数据类型对象包装类
一.概述 Sting s1 = "abc"; //s1是一个类类型变量,"abc"是一个对象. String s2 = new String(" ...
- 最简单的基于FFMPEG的封装格式转换器(无编解码)
本文介绍一个基于FFMPEG的封装格式转换器.所谓的封装格式转换,就是在AVI,FLV,MKV,MP4这些格式之间转换(相应.avi,.flv,.mkv,.mp4文件).须要注意的是,本程序并不进行视 ...
- Eclipse快捷键调试
Eclipse中有如下一些和运行调试相关的快捷键Ctrl+Shift+B:在当前行设置断点或取消设置的断点 F11:调试最后一次执行的程序 Ctrl+F11:运行最后一次执行的程序F5:跟踪到 ...
- chrome 全屏无法退出
起因:chrome 按F11 全屏后,再按F11 死活无法退出全屏,关掉后,再打开chrome,自动全屏,F11无法退出全屏 系统:centos 6.3 解决:找到 chrome的用户数据存储目录,( ...
- lucene索引并搜索mysql数据库[转]
由于对lucene比较感兴趣,本人在网上找了点资料,终于成功地用lucene对mysql数据库进行索引创建并成功搜索,先总结如下: 首先介绍一个jdbc工具类,用于得到Connection对象: im ...
- android 76 使用get post方式提交数据
get方式: package com.itheima.getmethod; import java.io.InputStream; import java.net.HttpURLConnection; ...
- iOS Runtime 实践(1)
很多时候我们都在看iOS开发中的黑魔法——Runtime.懂很多,但如何实践却少有人提及.本文便是iOS Runtime的实践第一篇. WebView 我们这次的实践主题,是使用针对接口编程的方式,借 ...
- java web 一次请求从开始到响应结束的过程
博客原文: http://www.cnblogs.com/yin-jingyu/archive/2011/08/01/2123548.html HTTP(HyperText Transfer ...
- iOS开发中常用的手势---边缘手势
说明:以下方法是开发中使用的方法,有什么不对的或者好的方法,请多多指教! 此处的边缘手势是用来控制左侧抽屉视图的弹出以及收回. 添加手势 : 页面上有多个手势时需要遵循 UIGestureRecogn ...