1 | <?php |
||
2 | /** |
||
3 | * @link http://www.yiiframework.com/ |
||
4 | * @copyright Copyright (c) 2008 Yii Software LLC |
||
5 | * @license http://www.yiiframework.com/license/ |
||
6 | */ |
||
7 | |||
8 | namespace yii\base; |
||
9 | |||
10 | use Yii; |
||
11 | |||
12 | /** |
||
13 | * Controller is the base class for classes containing controller logic. |
||
14 | * |
||
15 | * For more details and usage information on Controller, see the [guide article on controllers](guide:structure-controllers). |
||
16 | * |
||
17 | * @property Module[] $modules All ancestor modules that this controller is located within. This property is |
||
18 | * read-only. |
||
19 | * @property string $route The route (module ID, controller ID and action ID) of the current request. This |
||
20 | * property is read-only. |
||
21 | * @property string $uniqueId The controller ID that is prefixed with the module ID (if any). This property is |
||
22 | * read-only. |
||
23 | * @property View|\yii\web\View $view The view object that can be used to render views or view files. |
||
24 | * @property string $viewPath The directory containing the view files for this controller. |
||
25 | * |
||
26 | * @author Qiang Xue <[email protected]> |
||
27 | * @since 2.0 |
||
28 | */ |
||
29 | class Controller extends Component implements ViewContextInterface |
||
30 | { |
||
31 | /** |
||
32 | * @event ActionEvent an event raised right before executing a controller action. |
||
33 | * You may set [[ActionEvent::isValid]] to be false to cancel the action execution. |
||
34 | */ |
||
35 | const EVENT_BEFORE_ACTION = 'beforeAction'; |
||
36 | /** |
||
37 | * @event ActionEvent an event raised right after executing a controller action. |
||
38 | */ |
||
39 | const EVENT_AFTER_ACTION = 'afterAction'; |
||
40 | |||
41 | /** |
||
42 | * @var string the ID of this controller. |
||
43 | */ |
||
44 | public $id; |
||
45 | /** |
||
46 | * @var Module the module that this controller belongs to. |
||
47 | */ |
||
48 | public $module; |
||
49 | /** |
||
50 | * @var string the ID of the action that is used when the action ID is not specified |
||
51 | * in the request. Defaults to 'index'. |
||
52 | */ |
||
53 | public $defaultAction = 'index'; |
||
54 | /** |
||
55 | * @var null|string|false the name of the layout to be applied to this controller's views. |
||
56 | * This property mainly affects the behavior of [[render()]]. |
||
57 | * Defaults to null, meaning the actual layout value should inherit that from [[module]]'s layout value. |
||
58 | * If false, no layout will be applied. |
||
59 | */ |
||
60 | public $layout; |
||
61 | /** |
||
62 | * @var Action the action that is currently being executed. This property will be set |
||
63 | * by [[run()]] when it is called by [[Application]] to run an action. |
||
64 | */ |
||
65 | public $action; |
||
66 | |||
67 | /** |
||
68 | * @var View the view object that can be used to render views or view files. |
||
69 | */ |
||
70 | private $_view; |
||
71 | /** |
||
72 | * @var string the root directory that contains view files for this controller. |
||
73 | */ |
||
74 | private $_viewPath; |
||
75 | |||
76 | |||
77 | /** |
||
78 | * @param string $id the ID of this controller. |
||
79 | * @param Module $module the module that this controller belongs to. |
||
80 | * @param array $config name-value pairs that will be used to initialize the object properties. |
||
81 | */ |
||
82 | 376 | public function __construct($id, $module, $config = []) |
|
83 | { |
||
84 | 376 | $this->id = $id; |
|
85 | 376 | $this->module = $module; |
|
86 | 376 | parent::__construct($config); |
|
87 | 376 | } |
|
88 | |||
89 | /** |
||
90 | * Declares external actions for the controller. |
||
91 | * |
||
92 | * This method is meant to be overwritten to declare external actions for the controller. |
||
93 | * It should return an array, with array keys being action IDs, and array values the corresponding |
||
94 | * action class names or action configuration arrays. For example, |
||
95 | * |
||
96 | * ```php |
||
97 | * return [ |
||
98 | * 'action1' => 'app\components\Action1', |
||
99 | * 'action2' => [ |
||
100 | * 'class' => 'app\components\Action2', |
||
101 | * 'property1' => 'value1', |
||
102 | * 'property2' => 'value2', |
||
103 | * ], |
||
104 | * ]; |
||
105 | * ``` |
||
106 | * |
||
107 | * [[\Yii::createObject()]] will be used later to create the requested action |
||
108 | * using the configuration provided here. |
||
109 | */ |
||
110 | 240 | public function actions() |
|
111 | { |
||
112 | 240 | return []; |
|
113 | } |
||
114 | |||
115 | /** |
||
116 | * Runs an action within this controller with the specified action ID and parameters. |
||
117 | * If the action ID is empty, the method will use [[defaultAction]]. |
||
118 | * @param string $id the ID of the action to be executed. |
||
119 | * @param array $params the parameters (name-value pairs) to be passed to the action. |
||
120 | * @return mixed the result of the action. |
||
121 | * @throws InvalidRouteException if the requested action ID cannot be resolved into an action successfully. |
||
122 | * @see createAction() |
||
123 | */ |
||
124 | 218 | public function runAction($id, $params = []) |
|
125 | { |
||
126 | 218 | $action = $this->createAction($id); |
|
127 | 218 | if ($action === null) { |
|
128 | throw new InvalidRouteException('Unable to resolve the request: ' . $this->getUniqueId() . '/' . $id); |
||
129 | } |
||
130 | |||
131 | 218 | Yii::debug('Route to run: ' . $action->getUniqueId(), __METHOD__); |
|
132 | |||
133 | 218 | if (Yii::$app->requestedAction === null) { |
|
134 | 218 | Yii::$app->requestedAction = $action; |
|
135 | } |
||
136 | |||
137 | 218 | $oldAction = $this->action; |
|
138 | 218 | $this->action = $action; |
|
139 | |||
140 | 218 | $modules = []; |
|
141 | 218 | $runAction = true; |
|
142 | |||
143 | // call beforeAction on modules |
||
144 | 218 | foreach ($this->getModules() as $module) { |
|
145 | 218 | if ($module->beforeAction($action)) { |
|
146 | 218 | array_unshift($modules, $module); |
|
147 | } else { |
||
148 | $runAction = false; |
||
149 | 218 | break; |
|
150 | } |
||
151 | } |
||
152 | |||
153 | 218 | $result = null; |
|
154 | |||
155 | 218 | if ($runAction && $this->beforeAction($action)) { |
|
156 | // run the action |
||
157 | 216 | $result = $action->runWithParams($params); |
|
158 | |||
159 | 210 | $result = $this->afterAction($action, $result); |
|
160 | |||
161 | // call afterAction on modules |
||
162 | 210 | foreach ($modules as $module) { |
|
163 | /* @var $module Module */ |
||
164 | 210 | $result = $module->afterAction($action, $result); |
|
165 | } |
||
166 | } |
||
167 | |||
168 | 210 | if ($oldAction !== null) { |
|
169 | 5 | $this->action = $oldAction; |
|
170 | } |
||
171 | |||
172 | 210 | return $result; |
|
173 | } |
||
174 | |||
175 | /** |
||
176 | * Runs a request specified in terms of a route. |
||
177 | * The route can be either an ID of an action within this controller or a complete route consisting |
||
178 | * of module IDs, controller ID and action ID. If the route starts with a slash '/', the parsing of |
||
179 | * the route will start from the application; otherwise, it will start from the parent module of this controller. |
||
180 | * @param string $route the route to be handled, e.g., 'view', 'comment/view', '/admin/comment/view'. |
||
181 | * @param array $params the parameters to be passed to the action. |
||
182 | * @return mixed the result of the action. |
||
183 | * @see runAction() |
||
184 | */ |
||
185 | 198 | public function run($route, $params = []) |
|
186 | { |
||
187 | 198 | $pos = strpos($route, '/'); |
|
188 | 198 | if ($pos === false) { |
|
189 | 197 | return $this->runAction($route, $params); |
|
190 | 1 | } elseif ($pos > 0) { |
|
191 | 1 | return $this->module->runAction($route, $params); |
|
192 | } |
||
193 | |||
194 | return Yii::$app->runAction(ltrim($route, '/'), $params); |
||
195 | } |
||
196 | |||
197 | /** |
||
198 | * Binds the parameters to the action. |
||
199 | * This method is invoked by [[Action]] when it begins to run with the given parameters. |
||
200 | * @param Action $action the action to be bound with parameters. |
||
201 | * @param array $params the parameters to be bound to the action. |
||
202 | * @return array the valid parameters that the action can run with. |
||
203 | */ |
||
204 | 3 | public function bindActionParams($action, $params) |
|
205 | { |
||
206 | 3 | return []; |
|
207 | } |
||
208 | |||
209 | /** |
||
210 | * Creates an action based on the given action ID. |
||
211 | * The method first checks if the action ID has been declared in [[actions()]]. If so, |
||
212 | * it will use the configuration declared there to create the action object. |
||
213 | * If not, it will look for a controller method whose name is in the format of `actionXyz` |
||
214 | * where `xyz` is the action ID. If found, an [[InlineAction]] representing that |
||
215 | * method will be created and returned. |
||
216 | * @param string $id the action ID. |
||
217 | * @return Action|null the newly created action instance. Null if the ID doesn't resolve into any action. |
||
218 | */ |
||
219 | 233 | public function createAction($id) |
|
220 | { |
||
221 | 233 | if ($id === '') { |
|
222 | 3 | $id = $this->defaultAction; |
|
223 | } |
||
224 | |||
225 | 233 | $actionMap = $this->actions(); |
|
226 | 233 | if (isset($actionMap[$id])) { |
|
227 | 8 | return Yii::createObject($actionMap[$id], [$id, $this]); |
|
228 | 225 | } elseif (preg_match('/^[a-z0-9\\-_]+$/', $id) && strpos($id, '--') === false && trim($id, '-') === $id) { |
|
229 | 225 | $methodName = 'action' . str_replace(' ', '', ucwords(str_replace('-', ' ', $id))); |
|
230 | 225 | if (method_exists($this, $methodName)) { |
|
231 | 224 | $method = new \ReflectionMethod($this, $methodName); |
|
232 | 224 | if ($method->isPublic() && $method->getName() === $methodName) { |
|
233 | 224 | return new InlineAction($id, $this, $methodName); |
|
234 | } |
||
235 | } |
||
236 | } |
||
237 | |||
238 | 1 | return null; |
|
239 | } |
||
240 | |||
241 | /** |
||
242 | * This method is invoked right before an action is executed. |
||
243 | * |
||
244 | * The method will trigger the [[EVENT_BEFORE_ACTION]] event. The return value of the method |
||
245 | * will determine whether the action should continue to run. |
||
246 | * |
||
247 | * In case the action should not run, the request should be handled inside of the `beforeAction` code |
||
248 | * by either providing the necessary output or redirecting the request. Otherwise the response will be empty. |
||
249 | * |
||
250 | * If you override this method, your code should look like the following: |
||
251 | * |
||
252 | * ```php |
||
253 | * public function beforeAction($action) |
||
254 | * { |
||
255 | * // your custom code here, if you want the code to run before action filters, |
||
256 | * // which are triggered on the [[EVENT_BEFORE_ACTION]] event, e.g. PageCache or AccessControl |
||
257 | * |
||
258 | * if (!parent::beforeAction($action)) { |
||
259 | * return false; |
||
260 | * } |
||
261 | * |
||
262 | * // other custom code here |
||
263 | * |
||
264 | * return true; // or false to not run the action |
||
265 | * } |
||
266 | * ``` |
||
267 | * |
||
268 | * @param Action $action the action to be executed. |
||
269 | * @return bool whether the action should continue to run. |
||
270 | */ |
||
271 | 218 | public function beforeAction($action) |
|
272 | { |
||
273 | 218 | $event = new ActionEvent($action); |
|
274 | 218 | $this->trigger(self::EVENT_BEFORE_ACTION, $event); |
|
275 | 216 | return $event->isValid; |
|
276 | } |
||
277 | |||
278 | /** |
||
279 | * This method is invoked right after an action is executed. |
||
280 | * |
||
281 | * The method will trigger the [[EVENT_AFTER_ACTION]] event. The return value of the method |
||
282 | * will be used as the action return value. |
||
283 | * |
||
284 | * If you override this method, your code should look like the following: |
||
285 | * |
||
286 | * ```php |
||
287 | * public function afterAction($action, $result) |
||
288 | * { |
||
289 | * $result = parent::afterAction($action, $result); |
||
290 | * // your custom code here |
||
291 | * return $result; |
||
292 | * } |
||
293 | * ``` |
||
294 | * |
||
295 | * @param Action $action the action just executed. |
||
296 | * @param mixed $result the action return result. |
||
297 | * @return mixed the processed action result. |
||
298 | */ |
||
299 | 210 | public function afterAction($action, $result) |
|
300 | { |
||
301 | 210 | $event = new ActionEvent($action); |
|
302 | 210 | $event->result = $result; |
|
303 | 210 | $this->trigger(self::EVENT_AFTER_ACTION, $event); |
|
304 | 210 | return $event->result; |
|
305 | } |
||
306 | |||
307 | /** |
||
308 | * Returns all ancestor modules of this controller. |
||
309 | * The first module in the array is the outermost one (i.e., the application instance), |
||
310 | * while the last is the innermost one. |
||
311 | * @return Module[] all ancestor modules that this controller is located within. |
||
312 | */ |
||
313 | 218 | public function getModules() |
|
314 | { |
||
315 | 218 | $modules = [$this->module]; |
|
316 | 218 | $module = $this->module; |
|
317 | 218 | while ($module->module !== null) { |
|
318 | array_unshift($modules, $module->module); |
||
319 | $module = $module->module; |
||
320 | } |
||
321 | |||
322 | 218 | return $modules; |
|
323 | } |
||
324 | |||
325 | /** |
||
326 | * Returns the unique ID of the controller. |
||
327 | * @return string the controller ID that is prefixed with the module ID (if any). |
||
328 | */ |
||
329 | 248 | public function getUniqueId() |
|
330 | { |
||
331 | 248 | return $this->module instanceof Application ? $this->id : $this->module->getUniqueId() . '/' . $this->id; |
|
332 | } |
||
333 | |||
334 | /** |
||
335 | * Returns the route of the current request. |
||
336 | * @return string the route (module ID, controller ID and action ID) of the current request. |
||
337 | */ |
||
338 | 4 | public function getRoute() |
|
339 | { |
||
340 | 4 | return $this->action !== null ? $this->action->getUniqueId() : $this->getUniqueId(); |
|
341 | } |
||
342 | |||
343 | /** |
||
344 | * Renders a view and applies layout if available. |
||
345 | * |
||
346 | * The view to be rendered can be specified in one of the following formats: |
||
347 | * |
||
348 | * - [path alias](guide:concept-aliases) (e.g. "@app/views/site/index"); |
||
349 | * - absolute path within application (e.g. "//site/index"): the view name starts with double slashes. |
||
350 | * The actual view file will be looked for under the [[Application::viewPath|view path]] of the application. |
||
351 | * - absolute path within module (e.g. "/site/index"): the view name starts with a single slash. |
||
352 | * The actual view file will be looked for under the [[Module::viewPath|view path]] of [[module]]. |
||
353 | * - relative path (e.g. "index"): the actual view file will be looked for under [[viewPath]]. |
||
354 | * |
||
355 | * To determine which layout should be applied, the following two steps are conducted: |
||
356 | * |
||
357 | * 1. In the first step, it determines the layout name and the context module: |
||
358 | * |
||
359 | * - If [[layout]] is specified as a string, use it as the layout name and [[module]] as the context module; |
||
360 | * - If [[layout]] is null, search through all ancestor modules of this controller and find the first |
||
361 | * module whose [[Module::layout|layout]] is not null. The layout and the corresponding module |
||
362 | * are used as the layout name and the context module, respectively. If such a module is not found |
||
363 | * or the corresponding layout is not a string, it will return false, meaning no applicable layout. |
||
364 | * |
||
365 | * 2. In the second step, it determines the actual layout file according to the previously found layout name |
||
366 | * and context module. The layout name can be: |
||
367 | * |
||
368 | * - a [path alias](guide:concept-aliases) (e.g. "@app/views/layouts/main"); |
||
369 | * - an absolute path (e.g. "/main"): the layout name starts with a slash. The actual layout file will be |
||
370 | * looked for under the [[Application::layoutPath|layout path]] of the application; |
||
371 | * - a relative path (e.g. "main"): the actual layout file will be looked for under the |
||
372 | * [[Module::layoutPath|layout path]] of the context module. |
||
373 | * |
||
374 | * If the layout name does not contain a file extension, it will use the default one `.php`. |
||
375 | * |
||
376 | * @param string $view the view name. |
||
377 | * @param array $params the parameters (name-value pairs) that should be made available in the view. |
||
378 | * These parameters will not be available in the layout. |
||
379 | * @return string the rendering result. |
||
380 | * @throws InvalidArgumentException if the view file or the layout file does not exist. |
||
381 | */ |
||
382 | 7 | public function render($view, $params = []) |
|
383 | { |
||
384 | 7 | $content = $this->getView()->render($view, $params, $this); |
|
385 | 6 | return $this->renderContent($content); |
|
386 | } |
||
387 | |||
388 | /** |
||
389 | * Renders a static string by applying a layout. |
||
390 | * @param string $content the static string being rendered |
||
391 | * @return string the rendering result of the layout with the given static string as the `$content` variable. |
||
392 | * If the layout is disabled, the string will be returned back. |
||
393 | * @since 2.0.1 |
||
394 | */ |
||
395 | 6 | public function renderContent($content) |
|
396 | { |
||
397 | 6 | $layoutFile = $this->findLayoutFile($this->getView()); |
|
398 | 6 | if ($layoutFile !== false) { |
|
399 | 1 | return $this->getView()->renderFile($layoutFile, ['content' => $content], $this); |
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
400 | } |
||
401 | |||
402 | 5 | return $content; |
|
403 | } |
||
404 | |||
405 | /** |
||
406 | * Renders a view without applying layout. |
||
407 | * This method differs from [[render()]] in that it does not apply any layout. |
||
408 | * @param string $view the view name. Please refer to [[render()]] on how to specify a view name. |
||
409 | * @param array $params the parameters (name-value pairs) that should be made available in the view. |
||
410 | * @return string the rendering result. |
||
411 | * @throws InvalidArgumentException if the view file does not exist. |
||
412 | */ |
||
413 | public function renderPartial($view, $params = []) |
||
414 | { |
||
415 | return $this->getView()->render($view, $params, $this); |
||
416 | } |
||
417 | |||
418 | /** |
||
419 | * Renders a view file. |
||
420 | * @param string $file the view file to be rendered. This can be either a file path or a [path alias](guide:concept-aliases). |
||
421 | * @param array $params the parameters (name-value pairs) that should be made available in the view. |
||
422 | * @return string the rendering result. |
||
423 | * @throws InvalidArgumentException if the view file does not exist. |
||
424 | */ |
||
425 | 9 | public function renderFile($file, $params = []) |
|
426 | { |
||
427 | 9 | return $this->getView()->renderFile($file, $params, $this); |
|
428 | } |
||
429 | |||
430 | /** |
||
431 | * Returns the view object that can be used to render views or view files. |
||
432 | * The [[render()]], [[renderPartial()]] and [[renderFile()]] methods will use |
||
433 | * this view object to implement the actual view rendering. |
||
434 | * If not set, it will default to the "view" application component. |
||
435 | * @return View|\yii\web\View the view object that can be used to render views or view files. |
||
436 | */ |
||
437 | 16 | public function getView() |
|
438 | { |
||
439 | 16 | if ($this->_view === null) { |
|
440 | 16 | $this->_view = Yii::$app->getView(); |
|
441 | } |
||
442 | |||
443 | 16 | return $this->_view; |
|
444 | } |
||
445 | |||
446 | /** |
||
447 | * Sets the view object to be used by this controller. |
||
448 | * @param View|\yii\web\View $view the view object that can be used to render views or view files. |
||
449 | */ |
||
450 | public function setView($view) |
||
451 | { |
||
452 | $this->_view = $view; |
||
453 | } |
||
454 | |||
455 | /** |
||
456 | * Returns the directory containing view files for this controller. |
||
457 | * The default implementation returns the directory named as controller [[id]] under the [[module]]'s |
||
458 | * [[viewPath]] directory. |
||
459 | * @return string the directory containing the view files for this controller. |
||
460 | */ |
||
461 | 1 | public function getViewPath() |
|
462 | { |
||
463 | 1 | if ($this->_viewPath === null) { |
|
464 | 1 | $this->_viewPath = $this->module->getViewPath() . DIRECTORY_SEPARATOR . $this->id; |
|
465 | } |
||
466 | |||
467 | 1 | return $this->_viewPath; |
|
468 | } |
||
469 | |||
470 | /** |
||
471 | * Sets the directory that contains the view files. |
||
472 | * @param string $path the root directory of view files. |
||
473 | * @throws InvalidArgumentException if the directory is invalid |
||
474 | * @since 2.0.7 |
||
475 | */ |
||
476 | public function setViewPath($path) |
||
477 | { |
||
478 | $this->_viewPath = Yii::getAlias($path); |
||
0 ignored issues
–
show
It seems like
Yii::getAlias($path) can also be of type boolean . However, the property $_viewPath is declared as type string . Maybe add an additional type check?
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly. For example, imagine you have a variable Either this assignment is in error or a type check should be added for that assignment. class Id
{
public $id;
public function __construct($id)
{
$this->id = $id;
}
}
class Account
{
/** @var Id $id */
public $id;
}
$account_id = false;
if (starsAreRight()) {
$account_id = new Id(42);
}
$account = new Account();
if ($account instanceof Id)
{
$account->id = $account_id;
}
Loading history...
|
|||
479 | } |
||
480 | |||
481 | /** |
||
482 | * Finds the applicable layout file. |
||
483 | * @param View $view the view object to render the layout file. |
||
484 | * @return string|bool the layout file path, or false if layout is not needed. |
||
485 | * Please refer to [[render()]] on how to specify this parameter. |
||
486 | * @throws InvalidArgumentException if an invalid path alias is used to specify the layout. |
||
487 | */ |
||
488 | 6 | public function findLayoutFile($view) |
|
489 | { |
||
490 | 6 | $module = $this->module; |
|
491 | 6 | if (is_string($this->layout)) { |
|
492 | 1 | $layout = $this->layout; |
|
493 | 5 | } elseif ($this->layout === null) { |
|
494 | while ($module !== null && $module->layout === null) { |
||
495 | $module = $module->module; |
||
496 | } |
||
497 | if ($module !== null && is_string($module->layout)) { |
||
498 | $layout = $module->layout; |
||
499 | } |
||
500 | } |
||
501 | |||
502 | 6 | if (!isset($layout)) { |
|
503 | 5 | return false; |
|
504 | } |
||
505 | |||
506 | 1 | if (strncmp($layout, '@', 1) === 0) { |
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
507 | $file = Yii::getAlias($layout); |
||
508 | 1 | } elseif (strncmp($layout, '/', 1) === 0) { |
|
509 | $file = Yii::$app->getLayoutPath() . DIRECTORY_SEPARATOR . substr($layout, 1); |
||
510 | } else { |
||
511 | 1 | $file = $module->getLayoutPath() . DIRECTORY_SEPARATOR . $layout; |
|
512 | } |
||
513 | |||
514 | 1 | if (pathinfo($file, PATHINFO_EXTENSION) !== '') { |
|
515 | return $file; |
||
516 | } |
||
517 | 1 | $path = $file . '.' . $view->defaultExtension; |
|
518 | 1 | if ($view->defaultExtension !== 'php' && !is_file($path)) { |
|
519 | $path = $file . '.php'; |
||
520 | } |
||
521 | |||
522 | 1 | return $path; |
|
523 | } |
||
524 | } |
||
525 |