Action.php 2.4 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
/**
Qiang Xue committed
11
 * Action is the base class for all controller action classes.
Qiang Xue committed
12
 *
Qiang Xue committed
13
 * Action provides a way to divide a complex controller into
Qiang Xue committed
14 15
 * smaller actions in separate class files.
 *
Qiang Xue committed
16 17 18
 * 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
19
 * with user input values automatically according to their names.
Qiang Xue committed
20 21 22 23 24 25 26 27
 * 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
28 29
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
Qiang Xue committed
30
 * @since 2.0
Qiang Xue committed
31
 */
Qiang Xue committed
32
class Action extends Component
Qiang Xue committed
33 34
{
	/**
Qiang Xue committed
35
	 * @var string ID of the action
Qiang Xue committed
36
	 */
Qiang Xue committed
37
	public $id;
Qiang Xue committed
38
	/**
Qiang Xue committed
39
	 * @var Controller the controller that owns this action
Qiang Xue committed
40
	 */
Qiang Xue committed
41
	public $controller;
Qiang Xue committed
42 43

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

Qiang Xue committed
56 57 58 59 60 61 62 63 64
	/**
	 * 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
65
	/**
Qiang Xue committed
66 67
	 * Runs this action with the specified parameters.
	 * This method is mainly invoked by the controller.
Qiang Xue committed
68
	 * @param array $params the parameters to be bound to the action's run() method.
Qiang Xue committed
69
	 * @return integer the exit status (0 means normal, non-zero means abnormal).
Qiang Xue committed
70
	 * @throws InvalidConfigException if the action class does not have a run() method
Qiang Xue committed
71
	 */
Qiang Xue committed
72
	public function runWithParams($params)
Qiang Xue committed
73
	{
Qiang Xue committed
74 75
		if (!method_exists($this, 'run')) {
			throw new InvalidConfigException(get_class($this) . ' must define a "run()" method.');
Qiang Xue committed
76
		}
Qiang Xue committed
77 78
		$args = $this->controller->bindActionParams($this, $params);
		return (int)call_user_func_array(array($this, 'run'), $args);
Qiang Xue committed
79 80
	}
}