yii2框架随笔3
今天开始阅读vendor/yiisoft/yii2/base/Action.php
<?php namespace yii\base;//命名空间 use Yii;//加载Yii文件夹下的Yii.php /** * Action is the base class for all controller action classes. * * 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. * * Derived classes must implement a method named `run()`. This method * will be invoked by the controller when the action is requested. * The `run()` method can have parameters which will be filled up * with user input values automatically according to their names. * 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 */
/*此控制器是所有控制器动作的基类。
此控制器提供了一种方法可以重复使用操作方法的代码,此操作方法类可以在多个控制器或不同的项目中使用。
派生类必须实现一个名为run()的方法,这种方法在控制器被请求时调用。
举个例子:run()调用声明如下所示:public function run($id,$type=’book'){}并且提供了操作的参数 ['id'=>1];
所以当run(1),参数为1时自动调用run();
此属性只读。
*/
//此类继承Component类,下面我们先看一下Component是什么类。(请转下一段代码) class Action extends Component { /** * @var string ID of the action */ public $id; /** * @var Controller|\yii\web\Controller the controller that owns this action */ public $controller; /** * Constructor. * * @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. * * @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')) { 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__); if (Yii::$app->requestedParams === null) { Yii::$app->requestedParams = $args; } if ($this->beforeRun()) { $result = call_user_func_array([$this, 'run'], $args); $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. * * @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. */ protected function afterRun() { } }
我们接下来先重点了解一下Component.php
目录为:vendor/yiisoft/yii2/base/Component.php
<?php namespace yii\base; //命名空间 use Yii; //加载相关的类
/**
* Component is the base class that implements the *property*, *event* and *behavior* features.
*
* Component provides the *event* and *behavior* features, in addition to the *property* feature which is implemented in
* its parent class [[Object]].
*
* Event is a way to "inject" custom code into existing code at certain places. For example, a comment object can trigger
* an "add" event when the user adds a comment. We can write custom code and attach it to this event so that when the event
* is triggered (i.e. comment will be added), our custom code will be executed.
*
* An event is identified by a name that should be unique within the class it is defined at. Event names are *case-sensitive*.
*
* One or multiple PHP callbacks, called *event handlers*, can be attached to an event. You can call [[trigger()]] to
* raise an event. When an event is raised, the event handlers will be invoked automatically in the order they were
* attached.
*
* To attach an event handler to an event, call [[on()]]:
*/
//组件是实现属性,事件和行为特点的基类。
组件提供事件行为,这是继承他父类Object的特点。
事件是一种在特定的地方“注入”自定义代码到现有的代码。例如,
当一个用户添加评论事件时,注释的对象可以触发。我们可以编写自定义代码,并将其附加到此事件,这样当事件被触发(即评论将被添加),
我们的自定义代码会被执行,
一个或多个php回调,称为为‘事件处理’。要触发一个事件处理,可以用on();举个例子。
* ```php
* $post->on('update', function ($event) {
* // 发送电子邮件通知。
* });
在上述例子中,一个匿名函数被连接到'update'事件后。
可以用以下类型的程序做处理:
* - anonymous function: `function ($event) { ... }` ------匿名函数
* - object method: `[$object, 'handleAdd']` ------对象方法
* - static class method: `['Page', 'handleAdd']` ------静态类方法
* - global function: `'handleAdd'` ------全局函数
class Component extends Object { /** * @var array the attached event handlers (event name => handlers) */
//事件处理程序,是一个数组。(事件名称=>处理程序)
private $_events = []; /** * @var Behavior[]|null the attached behaviors (behavior name => behavior). This is `null` when not initialized. */
//行为为空时的连接形式,(行为名称=>行为),未初始化的时候为null.
private $_behaviors; /** * Returns the value of a component property. * This method will check in the following order and act accordingly: * * - a property defined by a getter: return the getter result * - a property of a behavior: return the behavior property value * * Do not call this method directly as it is a PHP magic method that * will be implicitly called when executing `$value = $component->property;`. * @param string $name the property name * @return mixed the property value or the value of a behavior's property * @throws UnknownPropertyException if the property is not defined * @throws InvalidCallException if the property is write-only. * @see __set() */
/ **
*返回一个组件的属性值。
*这个方法将检查以下顺序并采取相应的行动:
*
* - 通过一个getter定义的属性:返回getter的结果
* - 一个行为的属性:返回的行为属性值
*
*不要直接调用此方法,因为它是一个PHP魔术方法
*将执行`
$value = $component->property;时调用
`。
* @参数字符串$name属性名
* @返回混合属性值或行为的属性的值
*@throws UnknownPropertyException如果没有定义属性,抛出信息。
*@throws InvalidCallException如果该属性是只写的。
*@see__set()
public function __get($name) { $getter = 'get' . $name; //定义$getter if (method_exists($this, $getter)) { //如果$getter这种方法在这个对象中被调用,则返回TRUE. // read property, e.g. getName() return $this->$getter(); //调用$getter方法。 } else { // behavior property $this->ensureBehaviors(); //否则调用此函数,作用是确保此组件声明这种行为。 foreach ($this->_behaviors as $behavior) { if ($behavior->canGetProperty($name)) { //返回一个boolean类型的指示属性是否可读。 return $behavior->$name; //如果可读,返回name的值。 } } } if (method_exists($this, 'set' . $name)) {//如果set.$name这种方法在这个对象中被调用,则返回TRUE. throw new InvalidCallException('Getting write-only property: ' . get_class($this) . '::' . $name); //获取只读属性 } else { throw new UnknownPropertyException('Getting unknown property: ' . get_class($this) . '::' . $name); //如果没被调用,抛出异常信息:属性不明。(未完待续.....) } }