Action类,控制器中方法的基类:

  1. namespace yii\base;
  2.  
  3. use Yii;
  4.  
  5. /**
  6. * Action is the base class for all controller action classes.
  7. * Action是所有控制器方法的基类
  8. * Action provides a way to reuse action method code. An action method in an Action
  9. * class can be used in multiple controllers or in different projects.
  10. * Action提供了一种重用代码的方法,一个Action类中的方法可以被多个控制器或不同的项目调用
  11. * Derived classes must implement a method named `run()`. This method
  12. * will be invoked by the controller when the action is requested.
  13. * 驱动类必须实现run()方法,该方法在action被请求的时候被控制器调用
  14. * The `run()` method can have parameters which will be filled up
  15. * with user input values automatically according to their names.
  16. * 如果请求带有参数,run方法会自动带参数运行,例如:
  17. * For example, if the `run()` method is declared as follows:
  18. *
  19. * ```php
  20. * public function run($id, $type = 'book') { ... }
  21. * ```
  22. *
  23. * And the parameters provided for the action are: `['id' => 1]`.
  24. * Then the `run()` method will be invoked as `run(1)` automatically.
  25. *
  26. * @property string $uniqueId The unique ID of this action among the whole application. This property is
  27. * read-only.
  28. *
  29. * @author Qiang Xue <qiang.xue@gmail.com>
  30. * @since 2.0
  31. */
  32. class Action extends Component
  33. {
  34. /**
  35. * @var action的ID
  36. */
  37. public $id;
  38. /**
  39. * @var Controller|\yii\web\Controller 当前action的控制器
  40. */
  41. public $controller;
  42.  
  43. /**
  44. * Constructor.
  45. * 构造函数,用于初始化action 的ID和控制器,并且调用component的构造方法初始化对象
  46. *
  47. * @param string $id the ID of this action
  48. * @param Controller $controller the controller that owns this action
  49. * @param array $config name-value pairs that will be used to initialize the object properties
  50. */
  51. public function __construct($id, $controller, $config = [])
  52. {
  53. $this->id = $id;
  54. $this->controller = $controller;
  55. parent::__construct($config);
  56. }
  57.  
  58. /**
  59. * Returns the unique ID of this action among the whole application.
  60. * 用于取得当前action的唯一ID 形式为controller ID/action ID
  61. * @return string the unique ID of this action among the whole application.
  62. */
  63. public function getUniqueId()
  64. {
  65. return $this->controller->getUniqueId() . '/' . $this->id;
  66. }
  67.  
  68. /**
  69. * Runs this action with the specified parameters.
  70. * This method is mainly invoked by the controller.
  71. * 用指定的参数运行,该方法主要被控制器调用
  72. *
  73. * @param array $params the parameters to be bound to the action's run() method.
  74. * @return mixed the result of the action
  75. * @throws InvalidConfigException if the action class does not have a run() method
  76. */
  77. public function runWithParams($params)
  78. {
  79. if (!method_exists($this, 'run')) {//如果run方法不存在,抛出异常
  80. throw new InvalidConfigException(get_class($this) . ' must define a "run()" method.');
  81. }
  82. $args = $this->controller->bindActionParams($this, $params);//绑定参数
  83. Yii::trace('Running action: ' . get_class($this) . '::run()', __METHOD__);//记录trace信息
  84. if (Yii::$app->requestedParams === null) {
  85. Yii::$app->requestedParams = $args;//如果是命令行运行,按命令行方式获取参数
  86. }
  87. if ($this->beforeRun()) {//调用run的前置操作
  88. $result = call_user_func_array([$this, 'run'], $args);//用php内置函数带参数执行run方法
  89. $this->afterRun();//调用后置操作
  90.  
  91. return $result;//返回结果
  92. } else {
  93. return null;
  94. }
  95. }
  96.  
  97. /**
  98. * This method is called right before `run()` is executed.
  99. * You may override this method to do preparation work for the action run.
  100. * If the method returns false, it will cancel the action.
  101. * run方法的前置方法,通常在子类中重写来实现某些前置功能
  102. *
  103. * @return boolean whether to run the action.
  104. */
  105. protected function beforeRun()
  106. {
  107. return true;
  108. }
  109.  
  110. /**
  111. * This method is called right after `run()` is executed.
  112. * You may override this method to do post-processing work for the action run.
  113. * run方法的后置方法,通常在子类中重写来实现某些后置功能
  114. *
  115. */
  116. protected function afterRun()
  117. {
  118. }
  119. }

Yii源码阅读笔记(十二)的更多相关文章

  1. Yii源码阅读笔记(二十九)

    动态模型DynamicModel类,用于实现模型内数据验证: namespace yii\base; use yii\validators\Validator; /** * DynamicModel ...

  2. Yii源码阅读笔记(二十八)

    Yii/web中的Controller类,实现参数绑定,启动csrf验证功能,重定向页面功能: namespace yii\web; use Yii; use yii\base\InlineActio ...

  3. Yii源码阅读笔记(二十六)

    Application 类中设置路径的方法和调用ServiceLocator(服务定位器)加载运行时的组件的方法注释: /** * Handles the specified request. * 处 ...

  4. Yii源码阅读笔记(二十四)

    Module类中获取子模块,注册子模块,实例化控制器,根据路由运行指定控制器方法的注释: /** * Retrieves the child module of the specified ID. * ...

  5. Yii源码阅读笔记(二十二)

    Module类,属性的注释和构造函数的注释: <?php /** * @link http://www.yiiframework.com/ * @copyright Copyright (c) ...

  6. Yii源码阅读笔记(二十五)

    Module类中剩余部分代码,通过控制器ID实例化当前模块的控制器,当前模块的Action方法的前置和后置方法: /** * This method is invoked right before a ...

  7. Yii源码阅读笔记(二十)

    View中应用布局和缓存内容部分: /** * Begins recording a block. * This method is a shortcut to beginning [[Block]] ...

  8. Yii源码阅读笔记(二)

    接下来阅读BaseYii.php vendor/yiisoft/yii2/BaseYii.php—— namespace yii; use yii\base\InvalidConfigExceptio ...

  9. Yii源码阅读笔记(二十七)

    Theme 类,即一个应用的主题,主要通过替换路径实现主题的应用,里边的方法为获取根路径和根链接,以及应用主题的方法: namespace yii\base; use Yii; use yii\hel ...

  10. Yii源码阅读笔记(二十三)

    Module类中的辅助功能方法: /** * Returns an ID that uniquely identifies this module among all modules within t ...

随机推荐

  1. 微信api退款操作

    状况:证书加载进去,本地调试退款成功,然而发不到iis上却是不成功. 分析:定然是iis配置问题. 问题一:证书加载不进去,出现“内部错误” 解决:在iis中找到对应的应用连接池,右键高级设置,找到“ ...

  2. 使用show profiles分析SQL性能

    如何查看执行SQL的耗时 使用show profiles分析sql性能. Show profiles是5.0.37之后添加的,要想使用此功能,要确保版本在5.0.37之后. 查看数据库版本 mysql ...

  3. [转]Java Thread Dump 性能分析

    Java and Thread 一个 web 服务器使用几十到几百个线程来处理大量并发用户,如果一个或多个线程使用相同的资源,线程之间的竞争就不可避免了,并且有时候可能会发生死锁. Thread co ...

  4. 设计模式学习之简单工厂(Simple Factory,创建型模式)(1)

    简单工厂(Simple Factory,创建型模式) 第一步: 比如我们要采集苹果和香蕉,那么我们需要创建一个Apple类和Banana类,里面各自有采集方法get(),然后通过main方法进行调用, ...

  5. hdu 1195:Open the Lock(暴力BFS广搜)

    Open the Lock Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  6. Sql server之路 (三)添加本地数据库SDF文件

    12月25日 今天搞了半天 添加本地数据库Sdf文件到项目里.总是出现问题. 安装环境 Vs2008 没有安装的环境 1.Vs2008 sp1 2. 适用于 Windows 桌面的 Microsoft ...

  7. Scala中的Apply

    文章来自:http://www.cnblogs.com/hark0623/p/4194940.html  转载请注明 /** * Created by Administrator on 2014-12 ...

  8. ClassLoader类加载机制

    一.类加载器 类加载器(ClassLoader),顾名思义,即加载类的东西.在我们使用一个类之前,JVM需要先将该类的字节码文件(.class文件)从磁盘.网络或其他来源加载到内存中,并对字节码进行解 ...

  9. 餐厅到店点餐系统app燃尽图

    队友: 郭志豪:http://www.cnblogs.com/gzh13692021053/ 杨子健:http://www.cnblogs.com/yzj666/ 刘森松:http://www.cnb ...

  10. 简单几何(凸包) POJ 2187 Beauty Contest

    题目传送门 题意:求两点的距离平方的最大值 分析:凸包模板题 /************************************************ * Author :Running_T ...