标签:
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 /** 45 * Constructor. 46 * 构造函数,用于初始化action 的ID和控制器,并且调用component的构造方法初始化对象 47 * 48 * @param string $id the ID of this action 49 * @param Controller $controller the controller that owns this action 50 * @param array $config name-value pairs that will be used to initialize the object properties 51 */ 52 public function __construct($id, $controller, $config = []) 53 { 54 $this->id = $id; 55 $this->controller = $controller; 56 parent::__construct($config); 57 } 58 59 /** 60 * Returns the unique ID of this action among the whole application. 61 * 用于取得当前action的唯一ID 形式为controller ID/action ID 62 * @return string the unique ID of this action among the whole application. 63 */ 64 public function getUniqueId() 65 { 66 return $this->controller->getUniqueId() . ‘/‘ . $this->id; 67 } 68 69 /** 70 * Runs this action with the specified parameters. 71 * This method is mainly invoked by the controller. 72 * 用指定的参数运行,该方法主要被控制器调用 73 * 74 * @param array $params the parameters to be bound to the action‘s run() method. 75 * @return mixed the result of the action 76 * @throws InvalidConfigException if the action class does not have a run() method 77 */ 78 public function runWithParams($params) 79 { 80 if (!method_exists($this, ‘run‘)) {//如果run方法不存在,抛出异常 81 throw new InvalidConfigException(get_class($this) . ‘ must define a "run()" method.‘); 82 } 83 $args = $this->controller->bindActionParams($this, $params);//绑定参数 84 Yii::trace(‘Running action: ‘ . get_class($this) . ‘::run()‘, __METHOD__);//记录trace信息 85 if (Yii::$app->requestedParams === null) { 86 Yii::$app->requestedParams = $args;//如果是命令行运行,按命令行方式获取参数 87 } 88 if ($this->beforeRun()) {//调用run的前置操作 89 $result = call_user_func_array([$this, ‘run‘], $args);//用php内置函数带参数执行run方法 90 $this->afterRun();//调用后置操作 91 92 return $result;//返回结果 93 } else { 94 return null; 95 } 96 } 97 98 /** 99 * This method is called right before `run()` is executed. 100 * You may override this method to do preparation work for the action run. 101 * If the method returns false, it will cancel the action. 102 * run方法的前置方法,通常在子类中重写来实现某些前置功能 103 * 104 * @return boolean whether to run the action. 105 */ 106 protected function beforeRun() 107 { 108 return true; 109 } 110 111 /** 112 * This method is called right after `run()` is executed. 113 * You may override this method to do post-processing work for the action run. 114 * run方法的后置方法,通常在子类中重写来实现某些后置功能 115 * 116 */ 117 protected function afterRun() 118 { 119 } 120 }
标签:
原文地址:http://www.cnblogs.com/isleep/p/5415164.html