Action.php 2.57 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 28 29
 * For example, if the `run()` method is declared as follows:
 *
 * ~~~
 * public function run($id, $type = 'book') { ... }
 * ~~~
 *
 * And the parameters provided for the action are: `array('id' => 1)`.
 * Then the `run()` method will be invoked as `run(1)` automatically.
Qiang Xue committed
30 31
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
Qiang Xue committed
32
 * @since 2.0
Qiang Xue committed
33
 */
Qiang Xue committed
34
class Action extends Component
Qiang Xue committed
35 36
{
	/**
Qiang Xue committed
37
	 * @var string ID of the action
Qiang Xue committed
38
	 */
Qiang Xue committed
39
	public $id;
Qiang Xue committed
40
	/**
Qiang Xue committed
41
	 * @var Controller the controller that owns this action
Qiang Xue committed
42
	 */
Qiang Xue committed
43
	public $controller;
44

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

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