您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息
免费发信息

yii2源码学习笔记(八)

2024/4/29 17:27:40发布7次查看
action是所有控制器的基类,接下来了解一下它的源码。yii2\base\action.php
1 php 2 /** 3 * @link http://www.yiiframework.com/ 4 * @copyright copyright (c) 2008 yii software llc 5 * @license http://www.yiiframework.com/license/ 6 */ 7 8 namespace yii\base; 9 10 use yii; 11 12 /** 13 * action is the base class for all controller action classes. 14 * 是所有控制器的基类 15 * action provides a way to divide a complex controller into 16 * smaller actions in separate class files. 17 * 控制器提供了一种重复使用操作方法的代码,在多个控制器或不同的项目中使用 18 * derived classes must implement a method named `run()`. this method 19 * will be invoked by the controller when the action is requested. 20 * the `run()` method can have parameters which will be filled up 21 * with user input values automatically according to their names. 22 * 派生类必须实现一个名为run()的方法,这个方法会在控制器被请求时调用。 23 * 它可以有参数,将用户输入值的根据他们的名字自动填补。 24 * for example, if the `run()` method is declared as follows: 25 * 例:run()方法调用声明如下: 26 * ~~~ 27 * public function run($id, $type = 'book') { ... } 28 * ~~~ 29 * 30 * and the parameters provided for the action are: `['id' => 1]`. 31 * then the `run()` method will be invoked as `run(1)` automatically. 32 * 并且提供了操作的参数 ['id'=>1]; 33 * 当run(1)时自动调用run(); 34 * @property string $uniqueid the unique id of this action among the whole application. this property is 35 * read-only. 36 * 整个应用程序中,这一行动的唯一标识。此属性是只读 37 * @author qiang xue 38 * @since 2.0 39 */ 40 class action extends component 41 { 42 /** 43 * @var string id of the action id的动作 44 */ 45 public $id; 46 /** 47 * @var controller|\yii\web\controller the controller that owns this action 48 * 拥有这一行动的控制器 49 */ 50 public $controller; 51 52 53 /** 54 * constructor. 55 * 构造函数 56 * @param string $id the id of this action 这一行动的id 57 * @param controller $controller the controller that owns this action 拥有这一行动的控制器 58 * @param array $config name-value pairs that will be used to initialize the object properties 59 * 用来初始化对象属性的 name-value 60 */ 61 public function __construct($id, $controller, $config = []) 62 { 63 $this->id = $id; 64 $this->controller = $controller; 65 //调用父类的__construct()方法 66 parent::__construct($config); 67 } 68 69 /** 70 * returns the unique id of this action among the whole application. 71 * 返回整个应用程序中的唯一id。 72 * @return string the unique id of this action among the whole application. 73 * 在整个应用程序中,这一行动的唯一id。 74 */ 75 public function getuniqueid() 76 { 77 return $this->controller->getuniqueid() . '/' . $this->id; 78 } 79 80 /** 81 * runs this action with the specified parameters. 用指定的参数运行此操作。 82 * this method is mainly invoked by the controller. 该方法主要由控制器调用。 83 * 84 * @param array $params the parameters to be bound to the action's run() method.绑定到行动的run()方法的参数。 85 * @return mixed the result of the action 行动的结果 命名参数是否有效的 86 * @throws invalidconfigexception if the action class does not have a run() method 87 * 如果动作类没有run()方法 扔出异常 88 */ 89 public function runwithparams($params) 90 { 91 if (!method_exists($this, 'run')) {//如果动作类没有run()方法 抛出异常 92 throw new invalidconfigexception(get_class($this) . ' must define a run() method.'); 93 } 94 //调用bindactionparams()方法将参数绑定到动作。 95 $args = $this->controller->bindactionparams($this, $params); 96 //记录跟踪消息 97 yii::trace('running action: ' . get_class($this) . '::run()', __method__); 98 if (yii::$app->requestedparams === null) { 99 //请求的动作提供的参数100 yii::$app->requestedparams = $args;101 }102 if ($this->beforerun()) {103 //执行run()方法104 $result = call_user_func_array([$this, 'run'], $args);105 $this->afterrun();106 107 return $result;108 } else {109 return null;110 }111 }112 113 /**114 * this method is called right before `run()` is executed.115 * ` run() `执行前方法被调用。116 * you may override this method to do preparation work for the action run.117 * 可以重写此方法,为该操作运行的准备工作。118 * if the method returns false, it will cancel the action.119 * 如果该方法返回false,取消该操作。120 * @return boolean whether to run the action.121 */122 protected function beforerun()123 {124 return true;125 }126 127 /**128 * this method is called right after `run()` is executed. ` run() `执行后 方法被调用。129 * you may override this method to do post-processing work for the action run.130 * 可以重写此方法来处理该动作的后续处理工作。131 */132 protected function afterrun()133 {134 }135 }
接下来我们看一下事件参数相关重要的一个类actionevent。yii2\base\actionevent.php
1 php 2 /** 3 * @link http://www.yiiframework.com/ 4 * @copyright copyright (c) 2008 yii software llc 5 * @license http://www.yiiframework.com/license/ 6 */ 7 8 namespace yii\base; 9 10 /**11 * actionevent represents the event parameter used for an action event.12 * 用于操作事件的事件参数13 * by setting the [[isvalid]] property, one may control whether to continue running the action.14 * 通过设置[[isvalid]]属性,控制是否继续运行action。15 * @author qiang xue 16 * @since 2.017 */18 class actionevent extends event19 {20 /**21 * @var action the action currently being executed22 * 目前正在执行的行动23 */24 public $action;25 /**26 * @var mixed the action result. event handlers may modify this property to change the action result.27 * 操作结果 事件处理程序可以修改此属性来更改操作结果。28 */29 public $result;30 /**31 * @var boolean whether to continue running the action. event handlers of32 * [[controller::event_before_action]] may set this property to decide whether33 * to continue running the current action.34 * 是否继续运行该动作。设置[[controller::event_before_action]]属性决定是否执行当前的操作35 */36 public $isvalid = true;37 38 39 /**40 * constructor.构造函数。41 * @param action $action the action associated with this action event.与此事件相关联的动作。42 * @param array $config name-value pairs that will be used to initialize the object properties43 * 用来初始化对象属性的 name-value44 */45 public function __construct($action, $config = [])46 {47 $this->action = $action;48 parent::__construct($config);49 }50 }
今天最后看一下操作过滤器的基类吧actionfilter。yii2\base\actionfilter.php。
1 php 2 /** 3 * @link http://www.yiiframework.com/ 4 * @copyright copyright (c) 2008 yii software llc 5 * @license http://www.yiiframework.com/license/ 6 */ 7 8 namespace yii\base; 9 10 /** 11 * actionfilter is the base class for action filters. 12 * 是操作过滤器的基类。 13 * an action filter will participate in the action execution workflow by responding to 14 * the `beforeaction` and `afteraction` events triggered by modules and controllers. 15 * 一个操作过滤器将参与行动的执行工作流程,通过触发模型和控制器的`beforeaction` 和`afteraction` 事件 16 * check implementation of [[\yii\filters\accesscontrol]], [[\yii\filters\pagecache]] and [[\yii\filters\httpcache]] as examples on how to use it. 17 * 18 * @author qiang xue 19 * @since 2.0 20 */ 21 class actionfilter extends behavior 22 { 23 /** 24 * @var array list of action ids that this filter should apply to. if this property is not set, 25 * then the filter applies to all actions, unless they are listed in [[except]]. 26 * 操作标识列表。如果该属性未设置,过滤器适用于所有的行动,除非它们被列入[[except]]中。 27 * if an action id appears in both [[only]] and [[except]], this filter will not apply to it. 28 * 如果一个操作id 出现在[[only]] 和[[except]]中,该筛选器将不适用它 29 * note that if the filter is attached to a module, the action ids should also include child module ids (if any) 30 * and controller ids. 31 * 如果过滤器是链接到一个模块,操作检测还应包括子模块和控制器 32 * 33 * @see except 34 */ 35 public $only; 36 /** 37 * @var array list of action ids that this filter should not apply to. 38 * 此筛选器不应适用于操作id。 39 * @see only 40 */ 41 public $except = []; 42 43 44 /** 45 * @inheritdoc 46 * 将行为对象附加到组件。 47 */ 48 public function attach($owner) 49 { 50 $this->owner = $owner; 51 $owner->on(controller::event_before_action, [$this, 'beforefilter']); 52 } 53 54 /** 55 * @inheritdoc 56 * 将行为对象和组件分离。 57 */ 58 public function detach() 59 { 60 if ($this->owner) { 61 $this->owner->off(controller::event_before_action, [$this, 'beforefilter']); 62 $this->owner->off(controller::event_after_action, [$this, 'afterfilter']); 63 $this->owner = null; 64 } 65 } 66 67 /** 68 * @param actionevent $event 在动作之前调用 69 */ 70 public function beforefilter($event) 71 { 72 if (!$this->isactive($event->action)) { 73 return; 74 } 75 76 $event->isvalid = $this->beforeaction($event->action); 77 if ($event->isvalid) { 78 // call afterfilter only if beforefilter succeeds beforefilter 执行成功调用afterfilter 79 // beforefilter and afterfilter should be properly nested 两者要配合应用 80 $this->owner->on(controller::event_after_action, [$this, 'afterfilter'], null, false); 81 } else { 82 $event->handled = true; 83 } 84 } 85 86 /** 87 * @param actionevent $event 88 */ 89 public function afterfilter($event) 90 { 91 $event->result = $this->afteraction($event->action, $event->result); 92 $this->owner->off(controller::event_after_action, [$this, 'afterfilter']); 93 } 94 95 /** 96 * this method is invoked right before an action is to be executed (after all possible filters.) 97 * 此方法是在一个动作之前被调用的( 98 * you may override this method to do last-minute preparation for the action. 99 * @param action $action the action to be executed.要执行的动作100 * @return boolean whether the action should continue to be executed.101 * 是否应继续执行该动作。102 */103 public function beforeaction($action)104 {105 return true;106 }107 108 /**109 * this method is invoked right after an action is executed.110 * 此方法是在执行动作之后调用的。111 * you may override this method to do some postprocessing for the action.112 * @param action $action the action just executed. 刚刚执行的动作113 * @param mixed $result the action execution result 行动执行结果114 * @return mixed the processed action result. 处理结果。115 */116 public function afteraction($action, $result)117 {118 return $result;119 }120 121 /**122 * returns a value indicating whether the filer is active for the given action.123 * 返回一个值,给定的过滤器的行动是否为是积极的。124 * @param action $action the action being filtered 被过滤的动作125 * @return boolean whether the filer is active for the given action.126 * 给定的过滤器的行动是否为是积极的。127 */128 protected function isactive($action)129 {130 if ($this->owner instanceof module) {131 // convert action uniqueid into an id relative to the module132 $mid = $this->owner->getuniqueid();133 $id = $action->getuniqueid();134 if ($mid !== '' && strpos($id, $mid) === 0) {135 $id = substr($id, strlen($mid) + 1);136 }137 } else {138 $id = $action->id;139 }140 return !in_array($id, $this->except, true) && (empty($this->only) || in_array($id, $this->only, true));141 }142 }
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录