|
|
@ -42,31 +42,7 @@ class Controller extends Component implements Initable |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public $defaultAction = 'index'; |
|
|
|
public $defaultAction = 'index'; |
|
|
|
/** |
|
|
|
/** |
|
|
|
* @var Action the action that is currently being executed |
|
|
|
* @var array mapping from action ID to action configuration. |
|
|
|
*/ |
|
|
|
|
|
|
|
public $action; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* @param string $id ID of this controller |
|
|
|
|
|
|
|
* @param Module $module the module that this controller belongs to. |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public function __construct($id, $module) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
$this->id = $id; |
|
|
|
|
|
|
|
$this->module = $module; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Initializes the controller. |
|
|
|
|
|
|
|
* This method is called by the application before the controller starts to execute an action. |
|
|
|
|
|
|
|
* You may override this method to perform the needed initialization for the controller. |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public function init() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Returns a list of external action classes. |
|
|
|
|
|
|
|
* Array keys are action IDs, and array values are the corresponding |
|
|
|
* Array keys are action IDs, and array values are the corresponding |
|
|
|
* action class names or action configuration arrays. For example, |
|
|
|
* action class names or action configuration arrays. For example, |
|
|
|
* |
|
|
|
* |
|
|
@ -84,27 +60,41 @@ class Controller extends Component implements Initable |
|
|
|
* [[\Yii::createObject()]] will be invoked to create the requested action |
|
|
|
* [[\Yii::createObject()]] will be invoked to create the requested action |
|
|
|
* using the configuration provided here. |
|
|
|
* using the configuration provided here. |
|
|
|
* |
|
|
|
* |
|
|
|
* Derived classes may override this method to declare external actions. |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* Note, in order to inherit actions defined in the parent class, a child class needs to |
|
|
|
* Note, in order to inherit actions defined in the parent class, a child class needs to |
|
|
|
* merge the parent actions with child actions using functions like `array_merge()`. |
|
|
|
* merge the parent actions with child actions using functions like `array_merge()`. |
|
|
|
* |
|
|
|
|
|
|
|
* @return array list of external action classes |
|
|
|
|
|
|
|
* @see createAction |
|
|
|
* @see createAction |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function actions() |
|
|
|
public $actions = array(); |
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* @var Action the action that is currently being executed |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public $action; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* @param string $id ID of this controller |
|
|
|
|
|
|
|
* @param Module $module the module that this controller belongs to. |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public function __construct($id, $module) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
$this->id = $id; |
|
|
|
|
|
|
|
$this->module = $module; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Initializes the controller. |
|
|
|
|
|
|
|
* This method is called by the application before the controller starts to execute an action. |
|
|
|
|
|
|
|
* You may override this method to perform the needed initialization for the controller. |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public function init() |
|
|
|
{ |
|
|
|
{ |
|
|
|
return array(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Runs the controller with the specified action and parameters. |
|
|
|
* 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 |
|
|
|
* @param Action|string $action the action to be executed. This can be either an action object |
|
|
|
* or the ID of the action. |
|
|
|
* or the ID of the action. |
|
|
|
* @param array $params the parameters to be passed to the action. |
|
|
|
* @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. |
|
|
|
* 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. |
|
|
|
|
|
|
|
* @return integer the exit status of the action. 0 means normal, other values mean abnormal. |
|
|
|
* @return integer the exit status of the action. 0 means normal, other values mean abnormal. |
|
|
|
* @see missingAction |
|
|
|
* @see missingAction |
|
|
|
* @see createAction |
|
|
|
* @see createAction |
|
|
@ -122,32 +112,17 @@ class Controller extends Component implements Initable |
|
|
|
|
|
|
|
|
|
|
|
$priorAction = $this->action; |
|
|
|
$priorAction = $this->action; |
|
|
|
$this->action = $action; |
|
|
|
$this->action = $action; |
|
|
|
$exitStatus = 1; |
|
|
|
|
|
|
|
if ($this->authorize($action)) { |
|
|
|
if ($this->authorize($action) && $this->beforeAction($action)) { |
|
|
|
$params = $action->normalizeParams($params === null ? $this->getActionParams() : $params); |
|
|
|
$status = $action->runWithParams($params !== null ?: $this->getActionParams()); |
|
|
|
if ($params !== false) { |
|
|
|
|
|
|
|
if ($this->beforeAction($action)) { |
|
|
|
|
|
|
|
$exitStatus = (int)call_user_func_array(array($action, 'run'), $params); |
|
|
|
|
|
|
|
$this->afterAction($action); |
|
|
|
$this->afterAction($action); |
|
|
|
} |
|
|
|
|
|
|
|
} else { |
|
|
|
} else { |
|
|
|
$this->invalidActionParams($action); |
|
|
|
$status = 1; |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
$this->action = $priorAction; |
|
|
|
$this->action = $priorAction; |
|
|
|
return $exitStatus; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
return $status; |
|
|
|
* 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(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -163,15 +138,25 @@ class Controller extends Component implements Initable |
|
|
|
if ($actionID === '') { |
|
|
|
if ($actionID === '') { |
|
|
|
$actionID = $this->defaultAction; |
|
|
|
$actionID = $this->defaultAction; |
|
|
|
} |
|
|
|
} |
|
|
|
if (method_exists($this, 'action' . $actionID) && strcasecmp($actionID, 's')) { |
|
|
|
if (isset($this->actions[$actionID])) { |
|
|
|
|
|
|
|
return \Yii::createObject($this->actions[$actionID], $actionID, $this); |
|
|
|
|
|
|
|
} elseif (method_exists($this, 'action' . $actionID)) { |
|
|
|
return new InlineAction($actionID, $this); |
|
|
|
return new InlineAction($actionID, $this); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
$actions = $this->actions(); |
|
|
|
return null; |
|
|
|
if (isset($actions[$actionID])) { |
|
|
|
|
|
|
|
return \Yii::createObject($actions[$actionID], $actionID, $this); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
return null; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* 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(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -234,7 +219,7 @@ class Controller extends Component implements Initable |
|
|
|
if ($route[0] !== '/' && !$this->module instanceof Application) { |
|
|
|
if ($route[0] !== '/' && !$this->module instanceof Application) { |
|
|
|
$route = '/' . $this->module->getUniqueId() . '/' . $route; |
|
|
|
$route = '/' . $this->module->getUniqueId() . '/' . $route; |
|
|
|
} |
|
|
|
} |
|
|
|
$status = \Yii::$application->dispatch($route, $params); |
|
|
|
$status = \Yii::$application->processRequest($route, $params); |
|
|
|
} |
|
|
|
} |
|
|
|
if ($exit) { |
|
|
|
if ($exit) { |
|
|
|
\Yii::$application->end($status); |
|
|
|
\Yii::$application->end($status); |
|
|
|