You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
			
				
					81 lines
				
				2.4 KiB
			
		
		
			
		
	
	
					81 lines
				
				2.4 KiB
			| 
											14 years ago
										 | <?php
 | ||
|  | /**
 | ||
|  |  * @link http://www.yiiframework.com/
 | ||
| 
											13 years ago
										 |  * @copyright Copyright (c) 2008 Yii Software LLC
 | ||
| 
											14 years ago
										 |  * @license http://www.yiiframework.com/license/
 | ||
|  |  */
 | ||
|  | 
 | ||
| 
											14 years ago
										 | namespace yii\base;
 | ||
|  | 
 | ||
| 
											14 years ago
										 | /**
 | ||
| 
											14 years ago
										 |  * Action is the base class for all controller action classes.
 | ||
| 
											14 years ago
										 |  *
 | ||
| 
											14 years ago
										 |  * Action provides a way to divide a complex controller into
 | ||
| 
											14 years ago
										 |  * smaller actions in separate class files.
 | ||
|  |  *
 | ||
| 
											14 years ago
										 |  * 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
 | ||
| 
											14 years ago
										 |  * with user input values automatically according to their names.
 | ||
| 
											13 years ago
										 |  * 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.
 | ||
| 
											14 years ago
										 |  *
 | ||
|  |  * @author Qiang Xue <qiang.xue@gmail.com>
 | ||
| 
											14 years ago
										 |  * @since 2.0
 | ||
| 
											14 years ago
										 |  */
 | ||
| 
											14 years ago
										 | class Action extends Component
 | ||
| 
											14 years ago
										 | {
 | ||
|  | 	/**
 | ||
| 
											14 years ago
										 | 	 * @var string ID of the action
 | ||
| 
											14 years ago
										 | 	 */
 | ||
| 
											14 years ago
										 | 	public $id;
 | ||
| 
											14 years ago
										 | 	/**
 | ||
| 
											14 years ago
										 | 	 * @var Controller the controller that owns this action
 | ||
| 
											14 years ago
										 | 	 */
 | ||
| 
											14 years ago
										 | 	public $controller;
 | ||
| 
											14 years ago
										 | 
 | ||
|  | 	/**
 | ||
| 
											13 years ago
										 | 	 * Constructor.
 | ||
| 
											14 years ago
										 | 	 * @param string $id the ID of this action
 | ||
|  | 	 * @param Controller $controller the controller that owns this action
 | ||
| 
											13 years ago
										 | 	 * @param array $config name-value pairs that will be used to initialize the object properties
 | ||
| 
											14 years ago
										 | 	 */
 | ||
| 
											13 years ago
										 | 	public function __construct($id, $controller, $config = array())
 | ||
| 
											14 years ago
										 | 	{
 | ||
|  | 		$this->id = $id;
 | ||
|  | 		$this->controller = $controller;
 | ||
| 
											13 years ago
										 | 		parent::__construct($config);
 | ||
| 
											14 years ago
										 | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
| 
											13 years ago
										 | 	 * 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;
 | ||
|  | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
| 
											14 years ago
										 | 	 * Runs this action with the specified parameters.
 | ||
|  | 	 * This method is mainly invoked by the controller.
 | ||
| 
											13 years ago
										 | 	 * @param array $params the parameters to be bound to the action's run() method.
 | ||
| 
											14 years ago
										 | 	 * @return integer the exit status (0 means normal, non-zero means abnormal).
 | ||
| 
											13 years ago
										 | 	 * @throws InvalidConfigException if the action class does not have a run() method
 | ||
| 
											14 years ago
										 | 	 */
 | ||
| 
											14 years ago
										 | 	public function runWithParams($params)
 | ||
| 
											14 years ago
										 | 	{
 | ||
| 
											13 years ago
										 | 		if (!method_exists($this, 'run')) {
 | ||
|  | 			throw new InvalidConfigException(get_class($this) . ' must define a "run()" method.');
 | ||
| 
											14 years ago
										 | 		}
 | ||
| 
											13 years ago
										 | 		$args = $this->controller->bindActionParams($this, $params);
 | ||
|  | 		return (int)call_user_func_array(array($this, 'run'), $args);
 | ||
| 
											14 years ago
										 | 	}
 | ||
|  | }
 |