老刘 Yii2 源码学习笔记之 Action 类
Action 的概述

InlineAction 就是内联动作,所谓的内联动作就是放到controller 里面的 actionXXX 这种 Action。
customAction 就是独立动作,就是直接继承 Action 并实现 run 方法的 Action。
与 Controller 的交互
public function createAction($id)
{
if ($id === '') {
$id = $this->defaultAction;
} $actionMap = $this->actions();
if (isset($actionMap[$id])) {
return Yii::createObject($actionMap[$id], [$id, $this]);
} elseif (preg_match('/^[a-z0-9\\-_]+$/', $id) && strpos($id, '--') === false && trim($id, '-') === $id) {
$methodName = 'action' . str_replace(' ', '', ucwords(implode(' ', explode('-', $id))));
if (method_exists($this, $methodName)) {
$method = new \ReflectionMethod($this, $methodName);
if ($method->isPublic() && $method->getName() === $methodName) {
return new InlineAction($id, $this, $methodName);
}
}
} return null;
}
以上代码出自 yii\base\contoller。
createAction 首先通过 controller 的 actions 判断是否是独立的 Action, 如果是返回对象的实例,然后执行 runWithParams, 然后在执行 run 方法。这就是为什么独立的Action 都有实现 run 方法。下面是代码(yii\base\Action)
public function runWithParams($params)
{
if (!method_exists($this, 'run')) {
throw new InvalidConfigException(get_class($this) . ' must define a "run()" method.');
}
$args = $this->controller->bindActionParams($this, $params);
Yii::trace('Running action: ' . get_class($this) . '::run()', __METHOD__);
if (Yii::$app->requestedParams === null) {
Yii::$app->requestedParams = $args;
}
if ($this->beforeRun()) {
$result = call_user_func_array([$this, 'run'], $args);
$this->afterRun(); return $result;
} else {
return null;
}
}
如果不是独立的Action, 就会返回 InlineAction,它重写了runWithParams方法
public function runWithParams($params)
{
$args = $this->controller->bindActionParams($this, $params);
Yii::trace('Running action: ' . get_class($this->controller) . '::' . $this->actionMethod . '()', __METHOD__);
if (Yii::$app->requestedParams === null) {
Yii::$app->requestedParams = $args;
} return call_user_func_array([$this->controller, $this->actionMethod], $args);
}
总结
一开始看源码的时候,从 index.php 一直往下跟,结果看到Action的时候,有点乱了,$this 不知道指到什么地方去了,懵圈了。后来从 yii\base 开始看才看明白。欢迎讨论
老刘 Yii2 源码学习笔记之 Action 类的更多相关文章
- 老刘 Yii2 源码学习笔记之 Component 类
类图关系 属性与方法 class Component extends BaseObject { private $_events = []; private $_eventWildcards = [] ...
- 老刘 Yii2 源码学习笔记之 Module 类
关系类图 从上图可以看出 Application 类继承了 Module,在框架中的是非常重要角色. 加载配置 public function setModules($modules) { forea ...
- yii2源码学习笔记(九)
Application是所有应用程序类的基类,接下来了解一下它的源码.yii2\base\Application.php. <?php /** * @link http://www.yiifra ...
- yii2源码学习笔记(八)
Action是所有控制器的基类,接下来了解一下它的源码.yii2\base\Action.php <?php /** * @link http://www.yiiframework.com/ * ...
- yii2源码学习笔记(十一)
Controller控制器类,是所有控制器的基类,用于调用模型和布局. <?php /** * @link http://www.yiiframework.com/ * @copyright C ...
- yii2源码学习笔记(二十)
Widget类是所有部件的基类.yii2\base\Widget.php <?php /** * @link http://www.yiiframework.com/ * @copyright ...
- yii2源码学习笔记(十八)
View继承了component,用于渲染视图文件:yii2\base\View.php <?php /** * @link http://www.yiiframework.com/ * @co ...
- yii2源码学习笔记(十七)
Theme 类,应用的主题,通过替换路径实现主题的应用,方法为获取根路径和根链接:yii2\base\Theme.php <?php /** * @link http://www.yiifram ...
- yii2源码学习笔记(十四)
Module类是模块和应用类的基类. yiisoft\yii2\base\Module.php <?php /** * @link http://www.yiiframework.com/ * ...
随机推荐
- neo4j 学习-1
Neo4j 子句 ```MATCH (:Person { name: 'an' })-[r]->(:Persion) RETURN type(r) // 返回关系的类型 如:r 就是 'KNOW ...
- Cookie的Domain属性
Cookie 加了Domain后就写不进去了(不加domain就可以写进去了) 本地测试的时候需要把domain换成localhost cookie跨域的问题,意思就是说A.com下能访问B.com域 ...
- Date.parse和new Date(str)的兼容性问题
Date.parse和new Date(str)的兼容性问题 Date '2015-05-04'是无法被各个浏览器中,使用new Date(str)来正确生成日期对象的. 正确的用法是'2015/05 ...
- stuff函数(转)
在上篇博文中提到了stuff函数 在这篇博文中对stuff函数进行了详解 本片博文系转载,但对原文顺序做了下调整 示例 以下示例在第一个字符串 abcdef 中删除从第 2 个位置(字符 b)开始的三 ...
- 二叉树的锯齿形层次遍历 · Binary Tree Zigzag Level Order Traversal
[抄题]: 给出一棵二叉树,返回其节点值的锯齿形层次遍历(先从左往右,下一层再从右往左,层与层之间交替进行) [思维问题]: 不知道反复切换要怎么做:用boolean normalOrder当作布尔型 ...
- [leetcode]496. Next Greater Element I下一个较大元素
You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of n ...
- Spring的2.5版本中提供了一种:p名称空间的注入(了解)
1. 步骤一:需要先引入 p 名称空间 * 在schema的名称空间中加入该行:xmlns:p="http://www.springframework.org/schema/p"( ...
- maven的pom.xml样例
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...
- jquery获取radio单选框的值
1.获取原有单选框的值 var value=$("input[name='is_setting']:checked").val(); 2.获取重选后的单选框的值 <tr> ...
- bluez蓝牙测试工具
http://blog.csdn.net/talkxin/article/details/50610984