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.

89 lines
2.6 KiB

13 years ago
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
13 years ago
* @license http://www.yiiframework.com/license/
*/
namespace yii\base;
use Yii;
13 years ago
/**
* Action is the base class for all controller action classes.
13 years ago
*
* Action provides a way to divide a complex controller into
13 years ago
* smaller actions in separate class files.
*
13 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
* with user input values automatically according to their names.
12 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.
13 years ago
*
* @property string $uniqueId The unique ID of this action among the whole application.
*
13 years ago
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
13 years ago
*/
class Action extends Component
13 years ago
{
/**
* @var string ID of the action
13 years ago
*/
public $id;
13 years ago
/**
* @var Controller the controller that owns this action
13 years ago
*/
public $controller;
13 years ago
/**
12 years ago
* Constructor.
13 years ago
* @param string $id the ID of this action
* @param Controller $controller the controller that owns this action
* @param array $config name-value pairs that will be used to initialize the object properties
13 years ago
*/
public function __construct($id, $controller, $config = array())
13 years ago
{
$this->id = $id;
$this->controller = $controller;
parent::__construct($config);
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;
}
/**
13 years ago
* Runs this action with the specified parameters.
* This method is mainly invoked by the controller.
12 years ago
* @param array $params the parameters to be bound to the action's run() method.
* @return mixed the result of the action
12 years ago
* @throws InvalidConfigException if the action class does not have a run() method
13 years ago
*/
13 years ago
public function runWithParams($params)
13 years ago
{
12 years ago
if (!method_exists($this, 'run')) {
throw new InvalidConfigException(get_class($this) . ' must define a "run()" method.');
13 years ago
}
12 years ago
$args = $this->controller->bindActionParams($this, $params);
Yii::trace('Running action: ' . get_class($this) . '::run()', __METHOD__);
12 years ago
if (Yii::$app->requestedParams === null) {
Yii::$app->requestedParams = $args;
}
return call_user_func_array(array($this, 'run'), $args);
13 years ago
}
}