Action.php 3.53 KB
Newer Older
Qiang Xue committed
1 2
<?php
/**
Carsten Brandt committed
3
 * @link http://www.yiiframework.com/
Qiang Xue committed
4
 * @copyright Copyright (c) 2008 Yii Software LLC
Carsten Brandt committed
5
 * @license http://www.yiiframework.com/license/
Qiang Xue committed
6 7
 */

Qiang Xue committed
8 9
namespace yii\base;

Qiang Xue committed
10 11
use Yii;

Qiang Xue committed
12
/**
Qiang Xue committed
13
 * Action is the base class for all controller action classes.
Qiang Xue committed
14
 *
Qiang Xue committed
15
 * Action provides a way to divide a complex controller into
Qiang Xue committed
16 17
 * smaller actions in separate class files.
 *
Qiang Xue committed
18 19 20
 * 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
21
 * with user input values automatically according to their names.
Qiang Xue committed
22 23 24 25 26 27
 * For example, if the `run()` method is declared as follows:
 *
 * ~~~
 * public function run($id, $type = 'book') { ... }
 * ~~~
 *
Alexander Makarov committed
28
 * And the parameters provided for the action are: `['id' => 1]`.
Qiang Xue committed
29
 * Then the `run()` method will be invoked as `run(1)` automatically.
Qiang Xue committed
30
 *
31 32
 * @property string $uniqueId The unique ID of this action among the whole application. This property is
 * read-only.
33
 *
Qiang Xue committed
34
 * @author Qiang Xue <qiang.xue@gmail.com>
35
 * @since 2.0
Qiang Xue committed
36
 */
Qiang Xue committed
37
class Action extends Component
Qiang Xue committed
38
{
39 40 41 42 43 44 45 46
    /**
     * @var string ID of the action
     */
    public $id;
    /**
     * @var Controller the controller that owns this action
     */
    public $controller;
47

48 49 50
    /**
     * Constructor.
     *
51
     * @param string $id the ID of this action
52
     * @param Controller $controller the controller that owns this action
53
     * @param array $config name-value pairs that will be used to initialize the object properties
54 55 56 57 58 59 60
     */
    public function __construct($id, $controller, $config = [])
    {
        $this->id = $id;
        $this->controller = $controller;
        parent::__construct($config);
    }
Qiang Xue committed
61

62 63 64 65 66 67 68 69 70
    /**
     * 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;
    }
Qiang Xue committed
71

72 73 74 75 76
    /**
     * 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.
77
     * @return mixed the result of the action
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
     * @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();
93

94 95 96 97 98
            return $result;
        } else {
            return null;
        }
    }
99

100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
    /**
     * 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()
    {
    }
Qiang Xue committed
119
}