Action类

原文地址: http://www.cnblogs.com/dragon16/p/5543886.html


Action是所有控制器的基类

yii2\base\Action.php

<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace yii\base;

use Yii;

/**
 * Action is the base class for all controller action classes.
 * 是所有控制器的基类
 * Action provides a way to divide a complex controller into
 * smaller actions in separate class files.
 * 控制器提供了一种重复使用操作方法的代码,在多个控制器或不同的项目中使用
 * 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.
 * 派生类必须实现一个名为run()的方法,这个方法会在控制器被请求时调用。
 * 它可以有参数,将用户输入值的根据他们的名字自动填补。
 * For example, if the `run()` method is declared as follows:
 * 例:run()方法调用声明如下:
 * ~~~
 * 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.
 * 并且提供了操作的参数 ['id'=>1];
 * 当run(1)时自动调用run();
 * @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 string ID of the action     ID的动作
     */
    public $id;
    /**
     * @var Controller|\yii\web\Controller the controller that owns this action
     * 拥有这一行动的控制器
     */
    public $controller;


    /**
     * Constructor.
     * 构造函数
     * @param string $id the ID of this action  这一行动的ID
     * @param Controller $controller the controller that owns this action 拥有这一行动的控制器
     * @param array $config name-value pairs that will be used to initialize the object properties
     * 用来初始化对象属性的 name-value 
     */
    public function __construct($id, $controller, $config = [])
    {
        $this->id = $id;
        $this->controller = $controller;
        //调用父类的__construct()方法
        parent::__construct($config);
    }

    /**
     * Returns the unique ID of this action among the whole application.
     * 返回整个应用程序中的唯一ID。
     * @return string the unique ID of this action among the whole application.
     * 在整个应用程序中,这一行动的唯一ID。
     */
    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.绑定到行动的run()方法的参数。
     * @return mixed the result of the action   行动的结果  命名参数是否有效的
     * @throws InvalidConfigException if the action class does not have a run() method
     * 如果动作类没有run()方法 扔出异常
     */
    public function runWithParams($params)
    {
        if (!method_exists($this, 'run')) {//如果动作类没有run()方法 抛出异常
            throw new InvalidConfigException(get_class($this) . ' must define a "run()" method.');
        }
        //调用bindActionParams()方法将参数绑定到动作。
        $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()) {
            //执行run()方法
            $result = call_user_func_array([$this, 'run'], $args);
            $this->afterRun();

            return $result;
        } else {
            return null;
        }
    }

    /**
     * This method is called right before `run()` is executed.
     * ` run() `执行前方法被调用。
     * You may override this method to do preparation work for the action run.
     * 可以重写此方法,为该操作运行的准备工作。
     * If the method returns false, it will cancel the action.
     * 如果该方法返回false,取消该操作。
     * @return boolean whether to run the action.
     */
    protected function beforeRun()
    {
        return true;
    }

    /**
     * This method is called right after `run()` is executed.       ` run() `执行后 方法被调用。
     * You may override this method to do post-processing work for the action run.
     * 可以重写此方法来处理该动作的后续处理工作。
     */
    protected function afterRun()
    {
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值