Yii源码阅读笔记(十二)

时间:2023-03-09 01:05:57
Yii源码阅读笔记(十二)

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()
     {
     }
 }