Yii2 framework backup
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.

301 lines
9.6 KiB

13 years ago
<?php
/**
* Controller class file.
*
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008 Yii Software LLC
13 years ago
* @license http://www.yiiframework.com/license/
*/
namespace yii\base;
/**
13 years ago
* Controller is the base class for classes containing controller logic.
13 years ago
*
13 years ago
* Controller implements the action life cycles, which consist of the following steps:
13 years ago
*
13 years ago
* 1. [[authorize]]
* 2. [[beforeAction]]
12 years ago
* 3. [[afterAction]]
13 years ago
*
13 years ago
* @property array $actionParams the request parameters (name-value pairs) to be used for action parameter binding
* @property string $route the route (module ID, controller ID and action ID) of the current request.
* @property string $uniqueId the controller ID that is prefixed with the module ID (if any).
13 years ago
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class Controller extends Component
13 years ago
{
/**
12 years ago
* @var string the ID of this controller
13 years ago
*/
public $id;
/**
* @var Module $module the module that this controller belongs to.
*/
public $module;
/**
12 years ago
* @var string the ID of the action that is used when the action ID is not specified
* in the request. Defaults to 'index'.
13 years ago
*/
public $defaultAction = 'index';
/**
12 years ago
* @var string|boolean the name of the layout to be applied to this controller's views.
* This property mainly affects the behavior of [[render()]].
* Defaults to null, meaning the actual layout value should inherit that from [[module]]'s layout value.
* If false, no layout will be applied.
*/
public $layout;
/**
* @var Action the action that is currently being executed. This property will be set
* by [[run()]] when it is called by [[Application]] to run an action.
*/
public $action;
/**
* @param string $id the ID of this controller
* @param Module $module the module that this controller belongs to.
* @param array $config name-value pairs that will be used to initialize the object properties
*/
public function __construct($id, $module, $config = array())
{
$this->id = $id;
$this->module = $module;
parent::__construct($config);
}
/**
* Declares external actions for the controller.
* This method is meant to be overwritten to declare external actions for the controller.
* It should return an array, with array keys being action IDs, and array values the corresponding
13 years ago
* action class names or action configuration arrays. For example,
*
* ~~~
* return array(
* 'action1' => '@application/components/Action1',
* 'action2' => array(
* 'class' => '@application/components/Action2',
* 'property1' => 'value1',
* 'property2' => 'value2',
* ),
* );
* ~~~
*
12 years ago
* [[\Yii::createObject()]] will be used later to create the requested action
13 years ago
* using the configuration provided here.
*/
12 years ago
public function actions()
13 years ago
{
12 years ago
return array();
13 years ago
}
/**
* Runs the controller with the specified action and parameters.
* @param Action|string $action the action to be executed. This can be either an action object
* or the ID of the action.
13 years ago
* @param array $params the parameters (name-value pairs) to be passed to the action.
* If null, the result of [[getActionParams()]] will be used as action parameters.
13 years ago
* @return integer the exit status of the action. 0 means normal, other values mean abnormal.
* @see missingAction
* @see createAction
13 years ago
*/
public function run($action, $params = null)
13 years ago
{
if (is_string($action)) {
if (($a = $this->createAction($action)) !== null) {
$action = $a;
} else {
$this->missingAction($action);
return 1;
}
13 years ago
}
$priorAction = $this->action;
$this->action = $action;
13 years ago
if ($this->authorize($action) && $this->beforeAction($action)) {
13 years ago
if ($params === null) {
$params = $this->getActionParams();
}
$status = $action->runWithParams($params);
13 years ago
$this->afterAction($action);
} else {
$status = 1;
13 years ago
}
13 years ago
$this->action = $priorAction;
13 years ago
13 years ago
return $status;
13 years ago
}
/**
13 years ago
* Creates the action instance based on the action ID.
13 years ago
* The action can be either an inline action or an object.
13 years ago
* The latter is created by looking up the action map specified in [[actions]].
* @param string $actionID ID of the action. If empty, it will take the value of [[defaultAction]].
* @return Action the action instance, null if the action does not exist.
13 years ago
* @see actions
*/
public function createAction($actionID)
{
if ($actionID === '') {
$actionID = $this->defaultAction;
}
$actions = $this->actions();
if (isset($actions[$actionID])) {
return \Yii::createObject($actions[$actionID], $actionID, $this);
13 years ago
} elseif (method_exists($this, 'action' . $actionID)) {
13 years ago
return new InlineAction($actionID, $this);
} else {
13 years ago
return null;
13 years ago
}
13 years ago
}
/**
* Returns the request parameters that will be used for action parameter binding.
* Default implementation simply returns an empty array.
* Child classes may override this method to customize the parameters to be provided
* for action parameter binding (e.g. `$_GET`).
* @return array the request parameters (name-value pairs) to be used for action parameter binding
*/
public function getActionParams()
{
return array();
13 years ago
}
/**
13 years ago
* This method is invoked when the request parameters do not satisfy the requirement of the specified action.
* The default implementation will throw an exception.
* @param Action $action the action being executed
13 years ago
* @param Exception $exception the exception about the invalid parameters
13 years ago
* @throws Exception whenever this method is invoked
13 years ago
*/
13 years ago
public function invalidActionParams($action, $exception)
13 years ago
{
13 years ago
throw $exception;
13 years ago
}
/**
13 years ago
* This method is invoked when extra parameters are provided to an action when it is executed.
* The default implementation does nothing.
* @param Action $action the action being executed
* @param array $expected the expected action parameters (name => value)
* @param array $actual the actual action parameters (name => value)
*/
public function extraActionParams($action, $expected, $actual)
{
}
/**
13 years ago
* Handles the request whose action is not recognized.
* This method is invoked when the controller cannot find the requested action.
* The default implementation simply throws an exception.
* @param string $actionID the missing action name
12 years ago
* @throws InvalidRequestException whenever this method is invoked
13 years ago
*/
public function missingAction($actionID)
{
12 years ago
throw new InvalidRequestException(\Yii::t('yii', 'The system is unable to find the requested action "{action}".',
13 years ago
array('{action}' => $actionID == '' ? $this->defaultAction : $actionID)));
}
/**
* @return string the controller ID that is prefixed with the module ID (if any).
*/
public function getUniqueId()
{
13 years ago
return $this->module instanceof Application ? $this->id : $this->module->getUniqueId() . '/' . $this->id;
13 years ago
}
/**
13 years ago
* Returns the route of the current request.
13 years ago
* @return string the route (module ID, controller ID and action ID) of the current request.
*/
public function getRoute()
{
13 years ago
return $this->action !== null ? $this->getUniqueId() . '/' . $this->action->id : $this->getUniqueId();
13 years ago
}
/**
* Processes the request using another controller action.
* @param string $route the route of the new controller action. This can be an action ID, or a complete route
13 years ago
* with module ID (optional in the current module), controller ID and action ID. If the former,
* the action is assumed to be located within the current controller.
* @param array $params the parameters to be passed to the action.
* If null, the result of [[getActionParams()]] will be used as action parameters.
* Note that the parameters must be name-value pairs with the names corresponding to
* the parameter names as declared by the action.
13 years ago
* @param boolean $exit whether to end the application after this call. Defaults to true.
*/
public function forward($route, $params = array(), $exit = true)
13 years ago
{
if (strpos($route, '/') === false) {
$status = $this->run($route, $params);
13 years ago
} else {
if ($route[0] !== '/' && !$this->module instanceof Application) {
$route = '/' . $this->module->getUniqueId() . '/' . $route;
13 years ago
}
13 years ago
$status = \Yii::$application->runController($route, $params);
13 years ago
}
if ($exit) {
13 years ago
\Yii::$application->end($status);
13 years ago
}
}
/**
* This method is invoked when checking the access for the action to be executed.
* @param Action $action the action to be executed.
* @return boolean whether the action is allowed to be executed.
*/
13 years ago
public function authorize($action)
{
$event = new ActionEvent($action);
$this->trigger(__METHOD__, $event);
return $event->isValid;
}
/**
13 years ago
* This method is invoked right before an action is to be executed (after all possible filters.)
* You may override this method to do last-minute preparation for the action.
* @param Action $action the action to be executed.
* @return boolean whether the action should continue to be executed.
13 years ago
*/
13 years ago
public function beforeAction($action)
13 years ago
{
$event = new ActionEvent($action);
$this->trigger(__METHOD__, $event);
return $event->isValid;
13 years ago
}
/**
* This method is invoked right after an action is executed.
* You may override this method to do some postprocessing for the action.
* @param Action $action the action just executed.
13 years ago
*/
13 years ago
public function afterAction($action)
{
13 years ago
$this->trigger(__METHOD__, new ActionEvent($action));
}
13 years ago
public function render($view, $params = array())
{
12 years ago
return $this->createView()->render($view, $params);
13 years ago
}
public function renderText($text)
{
13 years ago
return $this->createView()->renderText($text);
13 years ago
}
public function renderPartial($view, $params = array())
{
13 years ago
return $this->createView()->renderPartial($view, $params);
13 years ago
}
13 years ago
public function createView()
{
13 years ago
return new View($this);
13 years ago
}
}