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

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>
Qiang Xue committed
35
 * @since 2.0
Qiang Xue committed
36
 */
Qiang Xue committed
37
class Action extends Component
Qiang Xue committed
38 39
{
	/**
Qiang Xue committed
40
	 * @var string ID of the action
Qiang Xue committed
41
	 */
Qiang Xue committed
42
	public $id;
Qiang Xue committed
43
	/**
Qiang Xue committed
44
	 * @var Controller the controller that owns this action
Qiang Xue committed
45
	 */
Qiang Xue committed
46
	public $controller;
47

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

Qiang Xue committed
61 62 63 64 65 66 67 68 69
	/**
	 * 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
70
	/**
Qiang Xue committed
71 72
	 * Runs this action with the specified parameters.
	 * This method is mainly invoked by the controller.
Qiang Xue committed
73
	 * @param array $params the parameters to be bound to the action's run() method.
74
	 * @return mixed the result of the action
Qiang Xue committed
75
	 * @throws InvalidConfigException if the action class does not have a run() method
Qiang Xue committed
76
	 */
Qiang Xue committed
77
	public function runWithParams($params)
Qiang Xue committed
78
	{
Qiang Xue committed
79 80
		if (!method_exists($this, 'run')) {
			throw new InvalidConfigException(get_class($this) . ' must define a "run()" method.');
Qiang Xue committed
81
		}
Qiang Xue committed
82
		$args = $this->controller->bindActionParams($this, $params);
Qiang Xue committed
83
		Yii::trace('Running action: ' . get_class($this) . '::run()', __METHOD__);
Qiang Xue committed
84 85 86
		if (Yii::$app->requestedParams === null) {
			Yii::$app->requestedParams = $args;
		}
Alexander Makarov committed
87
		return call_user_func_array([$this, 'run'], $args);
Qiang Xue committed
88 89
	}
}