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

 namespace yii\base;

 use Yii;

 /**
  * Action is the base class for all controller action classes.
  * Action是所有控制器方法的基类
  * Action provides a way to reuse action method code. An action method in an Action
  * class can be used in multiple controllers or in different projects.
  * Action提供了一种重用代码的方法,一个Action类中的方法可以被多个控制器或不同的项目调用
  * Derived classes must implement a method named `run()`. This method
  * will be invoked by the controller when the action is requested.
  * 驱动类必须实现run()方法,该方法在action被请求的时候被控制器调用
  * The `run()` method can have parameters which will be filled up
  * with user input values automatically according to their names.
  * 如果请求带有参数,run方法会自动带参数运行,例如:
  * For example, if the `run()` method is declared as follows:
  *
  * ```php
  * public function run($id, $type = 'book') { ... }
  * ```
  *
  * And the parameters provided for the action are: `['id' => 1]`.
  * Then the `run()` method will be invoked as `run(1)` automatically.
  *
  * @property string $uniqueId The unique ID of this action among the whole application. This property is
  * read-only.
  *
  * @author Qiang Xue <qiang.xue@gmail.com>
  * @since 2.0
  */
 class Action extends Component
 {
     /**
      * @var action的ID
      */
     public $id;
     /**
      * @var Controller|\yii\web\Controller 当前action的控制器
      */
     public $controller;

     /**
      * Constructor.
      * 构造函数,用于初始化action 的ID和控制器,并且调用component的构造方法初始化对象
      *
      * @param string $id the ID of this action
      * @param Controller $controller the controller that owns this action
      * @param array $config name-value pairs that will be used to initialize the object properties
      */
     public function __construct($id, $controller, $config = [])
     {
         $this->id = $id;
         $this->controller = $controller;
         parent::__construct($config);
     }

     /**
      * Returns the unique ID of this action among the whole application.
      * 用于取得当前action的唯一ID  形式为controller ID/action ID
      * @return string the unique ID of this action among the whole application.
      */
     public function getUniqueId()
     {
         return $this->controller->getUniqueId() . '/' . $this->id;
     }

     /**
      * Runs this action with the specified parameters.
      * This method is mainly invoked by the controller.
      * 用指定的参数运行,该方法主要被控制器调用
      *
      * @param array $params the parameters to be bound to the action's run() method.
      * @return mixed the result of the action
      * @throws InvalidConfigException if the action class does not have a run() method
      */
     public function runWithParams($params)
     {
         if (!method_exists($this, 'run')) {//如果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__);//记录trace信息
         if (Yii::$app->requestedParams === null) {
             Yii::$app->requestedParams = $args;//如果是命令行运行,按命令行方式获取参数
         }
         if ($this->beforeRun()) {//调用run的前置操作
             $result = call_user_func_array([$this, 'run'], $args);//用php内置函数带参数执行run方法
             $this->afterRun();//调用后置操作

             return $result;//返回结果
         } else {
             return null;
         }
     }

     /**
      * This method is called right before `run()` is executed.
      * You may override this method to do preparation work for the action run.
      * If the method returns false, it will cancel the action.
      * run方法的前置方法,通常在子类中重写来实现某些前置功能
      *
      * @return boolean whether to run the action.
      */
     protected function beforeRun()
     {
         return true;
     }

     /**
      * This method is called right after `run()` is executed.
      * You may override this method to do post-processing work for the action run.
      * run方法的后置方法,通常在子类中重写来实现某些后置功能
      *
      */
     protected function afterRun()
     {
     }
 }

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. 设计模式学习之工厂方法(Factory Method,创建型模式)(2)

    接着上一讲中的简单工厂继续讲解,假如我们有了需要采集新的水果梨子,如果我们使用简单工厂中的方式的话,就会新增一个Pear类,然后实现Fruit类,然后修改FruitFactory类中获取实例的方法 g ...

  2. 蓝桥杯 算法训练 区间k大数查询(水题)

    算法训练 区间k大数查询 时间限制:1.0s   内存限制:256.0MB 问题描述 给定一个序列,每次询问序列中第l个数到第r个数中第K大的数是哪个. 输入格式 第一行包含一个数n,表示序列长度. ...

  3. hdu 1010:Tempter of the Bone(DFS + 奇偶剪枝)

    Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  4. Java程序员最常用的8个Java日志框架

    转自:http://www.codeceo.com/article/8-java-log-framework.html 作为一名Java程序员,我们开发了很多Java应用程序,包括桌面应用.WEB应用 ...

  5. Android开发之日历控件实现

    Android开发之日历控件实现:以下都是转载的. 日历控件 日历控件 日历控件 日历控件

  6. [转]说说C语言运算符的“优先级”与“结合性”

    补充自己的一点理解: 1.关于++i 与 i++的区别. ++i 和 i++如果是单独使用的语句,即二者后面均加上分号,或者其他单独使用的语句,没有任何区别.例如: for(i=0;i<100; ...

  7. BZOJ4327 : JSOI2012 玄武密码

    对所有询问串建立AC自动机. 然后将母串在AC自动机上跑,每走到一个点x,从x点出发沿着fail指针能到的所有前缀都是匹配成功的,暴力向上走,碰到走过的就break,这样每个点最多只会被标记一次. 时 ...

  8. robotium 新建 android 测试项目:

    注意:新建项目后再运行前一定要修改Manifest文件中的instrumentation 中的target package, 这个是测试的入口 1. 程序开始要通知系统我要测的app是什么 如何知道a ...

  9. Codeforces Round #192 (Div. 2) B. Road Construction

    #include <iostream> #include <vector> using namespace std; int main(){ int n,m; cin > ...

  10. 【BZOJ】3039: 玉蟾宫(DP/单调栈)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3039 每次看到我的提交都有点淡淡的忧伤T_T.. 看到此题我想到用前缀和维护点ij向左和向上能拓展的 ...