Action.php 2.79 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;

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 45 46
	/**
	 * @var Response
	 */
47 48
	private $_response;

49 50 51 52

	/**
	 * @return Response|\yii\console\Response|\yii\web\Response
	 */
53 54 55
	public function getResponse()
	{
		if ($this->_response === null) {
56 57
			// TODO use applications response factory here
			//$this->_response = new Response();
58 59 60 61
		}
		return $this->_response;
	}

62 63 64
	/**
	 * @param Response $response
	 */
65 66 67 68 69
	public function setResponse($response)
	{
		$this->_response = $response;
	}

Qiang Xue committed
70
	/**
Qiang Xue committed
71
	 * Constructor.
Qiang Xue committed
72 73
	 * @param string $id the ID of this action
	 * @param Controller $controller the controller that owns this action
Qiang Xue committed
74
	 * @param array $config name-value pairs that will be used to initialize the object properties
Qiang Xue committed
75
	 */
Qiang Xue committed
76
	public function __construct($id, $controller, $config = array())
Qiang Xue committed
77 78 79
	{
		$this->id = $id;
		$this->controller = $controller;
Qiang Xue committed
80
		parent::__construct($config);
Qiang Xue committed
81 82
	}

Qiang Xue committed
83 84 85 86 87 88 89 90 91
	/**
	 * 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
92
	/**
Qiang Xue committed
93 94
	 * Runs this action with the specified parameters.
	 * This method is mainly invoked by the controller.
Qiang Xue committed
95
	 * @param array $params the parameters to be bound to the action's run() method.
96
	 * @return mixed the result of the action
Qiang Xue committed
97
	 * @throws InvalidConfigException if the action class does not have a run() method
Qiang Xue committed
98
	 */
Qiang Xue committed
99
	public function runWithParams($params)
Qiang Xue committed
100
	{
Qiang Xue committed
101 102
		if (!method_exists($this, 'run')) {
			throw new InvalidConfigException(get_class($this) . ' must define a "run()" method.');
Qiang Xue committed
103
		}
Qiang Xue committed
104
		$args = $this->controller->bindActionParams($this, $params);
105
		return call_user_func_array(array($this, 'run'), $args);
Qiang Xue committed
106 107
	}
}