1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
<?php
/**
* Controller class file.
*
* @link http://www.yiiframework.com/
* @copyright Copyright © 2008-2012 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\base;
/**
* Controller is the base class for classes containing controller logic.
*
* Controller implements the action life cycles, which consist of the following steps:
*
* 1. [[authorize]]
* 2. [[beforeAction]]
* 3. [[beforeRender]]
* 4. [[afterRender]]
* 5. [[afterAction]]
*
* @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).
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class Controller extends Component implements Initable
{
/**
* @var string ID of this controller
*/
public $id;
/**
* @var Module $module the module that this controller belongs to.
*/
public $module;
/**
* @var string the name of the default action. Defaults to 'index'.
*/
public $defaultAction = 'index';
/**
* @var array mapping from action ID to action configuration.
* Array keys are action IDs, and array values are the corresponding
* 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',
* ),
* );
* ~~~
*
* [[\Yii::createObject()]] will be invoked to create the requested action
* using the configuration provided here.
*
* 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()`.
* @see createAction
*/
public $actions = array();
/**
* @var Action the action that is currently being executed
*/
public $action;
/**
* @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 layout specified by the [[module]] should be used.
* If false, no layout will be applied.
*/
public $layout;
/**
* @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()
{
}
/**
* 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.
* @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.
* @return integer the exit status of the action. 0 means normal, other values mean abnormal.
* @see missingAction
* @see createAction
*/
public function run($action, $params = null)
{
if (is_string($action)) {
if (($a = $this->createAction($action)) !== null) {
$action = $a;
} else {
$this->missingAction($action);
return 1;
}
}
$priorAction = $this->action;
$this->action = $action;
if ($this->authorize($action) && $this->beforeAction($action)) {
if ($params === null) {
$params = $this->getActionParams();
}
$status = $action->runWithParams($params);
$this->afterAction($action);
} else {
$status = 1;
}
$this->action = $priorAction;
return $status;
}
/**
* Creates the action instance based on the action ID.
* The action can be either an inline action or an object.
* 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.
* @see actions
*/
public function createAction($actionID)
{
if ($actionID === '') {
$actionID = $this->defaultAction;
}
if (isset($this->actions[$actionID])) {
return \Yii::createObject($this->actions[$actionID], $actionID, $this);
} elseif (method_exists($this, 'action' . $actionID)) {
return new InlineAction($actionID, $this);
} else {
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();
}
/**
* 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
* @param Exception $exception the exception about the invalid parameters
* @throws Exception whenever this method is invoked
*/
public function invalidActionParams($action, $exception)
{
throw $exception;
}
/**
* 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)
{
}
/**
* 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
* @throws Exception whenever this method is invoked
*/
public function missingAction($actionID)
{
throw new Exception(\Yii::t('yii', 'The system is unable to find the requested action "{action}".',
array('{action}' => $actionID == '' ? $this->defaultAction : $actionID)));
}
/**
* @return string the controller ID that is prefixed with the module ID (if any).
*/
public function getUniqueId()
{
return $this->module instanceof Application ? $this->id : $this->module->getUniqueId() . '/' . $this->id;
}
/**
* Returns the route of the current request.
* @return string the route (module ID, controller ID and action ID) of the current request.
*/
public function getRoute()
{
return $this->action !== null ? $this->getUniqueId() . '/' . $this->action->id : $this->getUniqueId();
}
/**
* 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
* 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.
* @param boolean $exit whether to end the application after this call. Defaults to true.
*/
public function forward($route, $params = array(), $exit = true)
{
if (strpos($route, '/') === false) {
$status = $this->run($route, $params);
} else {
if ($route[0] !== '/' && !$this->module instanceof Application) {
$route = '/' . $this->module->getUniqueId() . '/' . $route;
}
$status = \Yii::$application->runController($route, $params);
}
if ($exit) {
\Yii::$application->end($status);
}
}
/**
* 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.
*/
public function authorize($action)
{
$event = new ActionEvent($action);
$this->trigger(__METHOD__, $event);
return $event->isValid;
}
/**
* 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.
*/
public function beforeAction($action)
{
$event = new ActionEvent($action);
$this->trigger(__METHOD__, $event);
return $event->isValid;
}
/**
* 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.
*/
public function afterAction($action)
{
$this->trigger(__METHOD__, new ActionEvent($action));
}
/**
* This method is invoked right before an action renders its result using [[render()]].
* @param string $view the view to be rendered
* @return boolean whether the action should continue to render.
*/
public function beforeRender($view)
{
$event = new RenderEvent($view);
$this->trigger(__METHOD__, $event);
return $event->isValid;
}
/**
* This method is invoked right after an action renders its result using [[render()]].
* @param string $view the view just rendered
* @param string $content the content to be displayed
* @return string the content to be displayed. This may be the same as the input content or the one
* modified by event handlers.
*/
public function afterRender($view, $content)
{
$event = new RenderEvent($view, $content);
$this->trigger(__METHOD__, $event);
return $event->content;
}
public function render($view, $params = array())
{
if ($this->beforeRender($view)) {
$content = $this->createView()->render($view, $params);
$content = $this->afterRender($view, $content);
return $content;
}
return '';
}
public function renderText($text)
{
return $this->createView()->renderText($text);
}
public function renderPartial($view, $params = array())
{
return $this->createView()->renderPartial($view, $params);
}
public function createView()
{
return new View($this);
}
}