Complex classes like Controller often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Controller, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
27 | class Controller extends Component implements ViewContextInterface |
||
28 | { |
||
29 | /** |
||
30 | * @event ActionEvent an event raised right before executing a controller action. |
||
31 | * You may set [[ActionEvent::isValid]] to be false to cancel the action execution. |
||
32 | */ |
||
33 | const EVENT_BEFORE_ACTION = 'beforeAction'; |
||
34 | /** |
||
35 | * @event ActionEvent an event raised right after executing a controller action. |
||
36 | */ |
||
37 | const EVENT_AFTER_ACTION = 'afterAction'; |
||
38 | |||
39 | /** |
||
40 | * @var string the ID of this controller. |
||
41 | */ |
||
42 | public $id; |
||
43 | /** |
||
44 | * @var Module $module the module that this controller belongs to. |
||
45 | */ |
||
46 | public $module; |
||
47 | /** |
||
48 | * @var string the ID of the action that is used when the action ID is not specified |
||
49 | * in the request. Defaults to 'index'. |
||
50 | */ |
||
51 | public $defaultAction = 'index'; |
||
52 | /** |
||
53 | * @var null|string|false the name of the layout to be applied to this controller's views. |
||
54 | * This property mainly affects the behavior of [[render()]]. |
||
55 | * Defaults to null, meaning the actual layout value should inherit that from [[module]]'s layout value. |
||
56 | * If false, no layout will be applied. |
||
57 | */ |
||
58 | public $layout; |
||
59 | /** |
||
60 | * @var Action the action that is currently being executed. This property will be set |
||
61 | * by [[run()]] when it is called by [[Application]] to run an action. |
||
62 | */ |
||
63 | public $action; |
||
64 | |||
65 | /** |
||
66 | * @var View the view object that can be used to render views or view files. |
||
67 | */ |
||
68 | private $_view; |
||
69 | /** |
||
70 | * @var string the root directory that contains view files for this controller. |
||
71 | */ |
||
72 | private $_viewPath; |
||
73 | |||
74 | |||
75 | /** |
||
76 | * @param string $id the ID of this controller. |
||
77 | * @param Module $module the module that this controller belongs to. |
||
78 | * @param array $config name-value pairs that will be used to initialize the object properties. |
||
79 | */ |
||
80 | 124 | public function __construct($id, $module, $config = []) |
|
86 | |||
87 | /** |
||
88 | * Declares external actions for the controller. |
||
89 | * This method is meant to be overwritten to declare external actions for the controller. |
||
90 | * It should return an array, with array keys being action IDs, and array values the corresponding |
||
91 | * action class names or action configuration arrays. For example, |
||
92 | * |
||
93 | * ```php |
||
94 | * return [ |
||
95 | * 'action1' => \app\components\Action1::class, |
||
96 | * 'action2' => [ |
||
97 | * 'class' => \app\components\Action2::class, |
||
98 | * 'property1' => 'value1', |
||
99 | * 'property2' => 'value2', |
||
100 | * ], |
||
101 | * ]; |
||
102 | * ``` |
||
103 | * |
||
104 | * [[\Yii::createObject()]] will be used later to create the requested action |
||
105 | * using the configuration provided here. |
||
106 | */ |
||
107 | 68 | public function actions() |
|
111 | |||
112 | /** |
||
113 | * Runs an action within this controller with the specified action ID and parameters. |
||
114 | * If the action ID is empty, the method will use [[defaultAction]]. |
||
115 | * @param string $id the ID of the action to be executed. |
||
116 | * @param array $params the parameters (name-value pairs) to be passed to the action. |
||
117 | * @return mixed the result of the action. |
||
118 | * @throws InvalidRouteException if the requested action ID cannot be resolved into an action successfully. |
||
119 | * @see createAction() |
||
120 | */ |
||
121 | 68 | public function runAction($id, $params = []) |
|
169 | |||
170 | /** |
||
171 | * Runs a request specified in terms of a route. |
||
172 | * The route can be either an ID of an action within this controller or a complete route consisting |
||
173 | * of module IDs, controller ID and action ID. If the route starts with a slash '/', the parsing of |
||
174 | * the route will start from the application; otherwise, it will start from the parent module of this controller. |
||
175 | * @param string $route the route to be handled, e.g., 'view', 'comment/view', '/admin/comment/view'. |
||
176 | * @param array $params the parameters to be passed to the action. |
||
177 | * @return mixed the result of the action. |
||
178 | * @see runAction() |
||
179 | */ |
||
180 | 66 | public function run($route, $params = []) |
|
191 | |||
192 | /** |
||
193 | * Binds the parameters to the action. |
||
194 | * This method is invoked by [[Action]] when it begins to run with the given parameters. |
||
195 | * @param Action $action the action to be bound with parameters. |
||
196 | * @param array $params the parameters to be bound to the action. |
||
197 | * @return array the valid parameters that the action can run with. |
||
198 | */ |
||
199 | 1 | public function bindActionParams($action, $params) |
|
203 | |||
204 | /** |
||
205 | * Creates an action based on the given action ID. |
||
206 | * The method first checks if the action ID has been declared in [[actions()]]. If so, |
||
207 | * it will use the configuration declared there to create the action object. |
||
208 | * If not, it will look for a controller method whose name is in the format of `actionXyz` |
||
209 | * where `Xyz` stands for the action ID. If found, an [[InlineAction]] representing that |
||
210 | * method will be created and returned. |
||
211 | * @param string $id the action ID. |
||
212 | * @return Action the newly created action instance. Null if the ID doesn't resolve into any action. |
||
213 | */ |
||
214 | 68 | public function createAction($id) |
|
235 | |||
236 | /** |
||
237 | * This method is invoked right before an action is executed. |
||
238 | * |
||
239 | * The method will trigger the [[EVENT_BEFORE_ACTION]] event. The return value of the method |
||
240 | * will determine whether the action should continue to run. |
||
241 | * |
||
242 | * In case the action should not run, the request should be handled inside of the `beforeAction` code |
||
243 | * by either providing the necessary output or redirecting the request. Otherwise the response will be empty. |
||
244 | * |
||
245 | * If you override this method, your code should look like the following: |
||
246 | * |
||
247 | * ```php |
||
248 | * public function beforeAction($action) |
||
249 | * { |
||
250 | * // your custom code here, if you want the code to run before action filters, |
||
251 | * // which are triggered on the [[EVENT_BEFORE_ACTION]] event, e.g. PageCache or AccessControl |
||
252 | * |
||
253 | * if (!parent::beforeAction($action)) { |
||
254 | * return false; |
||
255 | * } |
||
256 | * |
||
257 | * // other custom code here |
||
258 | * |
||
259 | * return true; // or false to not run the action |
||
260 | * } |
||
261 | * ``` |
||
262 | * |
||
263 | * @param Action $action the action to be executed. |
||
264 | * @return boolean whether the action should continue to run. |
||
265 | */ |
||
266 | 68 | public function beforeAction($action) |
|
272 | |||
273 | /** |
||
274 | * This method is invoked right after an action is executed. |
||
275 | * |
||
276 | * The method will trigger the [[EVENT_AFTER_ACTION]] event. The return value of the method |
||
277 | * will be used as the action return value. |
||
278 | * |
||
279 | * If you override this method, your code should look like the following: |
||
280 | * |
||
281 | * ```php |
||
282 | * public function afterAction($action, $result) |
||
283 | * { |
||
284 | * $result = parent::afterAction($action, $result); |
||
285 | * // your custom code here |
||
286 | * return $result; |
||
287 | * } |
||
288 | * ``` |
||
289 | * |
||
290 | * @param Action $action the action just executed. |
||
291 | * @param mixed $result the action return result. |
||
292 | * @return mixed the processed action result. |
||
293 | */ |
||
294 | 65 | public function afterAction($action, $result) |
|
301 | |||
302 | /** |
||
303 | * Returns all ancestor modules of this controller. |
||
304 | * The first module in the array is the outermost one (i.e., the application instance), |
||
305 | * while the last is the innermost one. |
||
306 | * @return Module[] all ancestor modules that this controller is located within. |
||
307 | */ |
||
308 | 68 | public function getModules() |
|
318 | |||
319 | /** |
||
320 | * Returns the unique ID of the controller. |
||
321 | * @return string the controller ID that is prefixed with the module ID (if any). |
||
322 | */ |
||
323 | 72 | public function getUniqueId() |
|
327 | |||
328 | /** |
||
329 | * Returns the route of the current request. |
||
330 | * @return string the route (module ID, controller ID and action ID) of the current request. |
||
331 | */ |
||
332 | 4 | public function getRoute() |
|
336 | |||
337 | /** |
||
338 | * Renders a view and applies layout if available. |
||
339 | * |
||
340 | * The view to be rendered can be specified in one of the following formats: |
||
341 | * |
||
342 | * - path alias (e.g. "@app/views/site/index"); |
||
343 | * - absolute path within application (e.g. "//site/index"): the view name starts with double slashes. |
||
344 | * The actual view file will be looked for under the [[Application::viewPath|view path]] of the application. |
||
345 | * - absolute path within module (e.g. "/site/index"): the view name starts with a single slash. |
||
346 | * The actual view file will be looked for under the [[Module::viewPath|view path]] of [[module]]. |
||
347 | * - relative path (e.g. "index"): the actual view file will be looked for under [[viewPath]]. |
||
348 | * |
||
349 | * To determine which layout should be applied, the following two steps are conducted: |
||
350 | * |
||
351 | * 1. In the first step, it determines the layout name and the context module: |
||
352 | * |
||
353 | * - If [[layout]] is specified as a string, use it as the layout name and [[module]] as the context module; |
||
354 | * - If [[layout]] is null, search through all ancestor modules of this controller and find the first |
||
355 | * module whose [[Module::layout|layout]] is not null. The layout and the corresponding module |
||
356 | * are used as the layout name and the context module, respectively. If such a module is not found |
||
357 | * or the corresponding layout is not a string, it will return false, meaning no applicable layout. |
||
358 | * |
||
359 | * 2. In the second step, it determines the actual layout file according to the previously found layout name |
||
360 | * and context module. The layout name can be: |
||
361 | * |
||
362 | * - a path alias (e.g. "@app/views/layouts/main"); |
||
363 | * - an absolute path (e.g. "/main"): the layout name starts with a slash. The actual layout file will be |
||
364 | * looked for under the [[Application::layoutPath|layout path]] of the application; |
||
365 | * - a relative path (e.g. "main"): the actual layout file will be looked for under the |
||
366 | * [[Module::layoutPath|layout path]] of the context module. |
||
367 | * |
||
368 | * If the layout name does not contain a file extension, it will use the default one `.php`. |
||
369 | * |
||
370 | * @param string $view the view name. |
||
371 | * @param array $params the parameters (name-value pairs) that should be made available in the view. |
||
372 | * These parameters will not be available in the layout. |
||
373 | * @return string the rendering result. |
||
374 | * @throws InvalidParamException if the view file or the layout file does not exist. |
||
375 | */ |
||
376 | public function render($view, $params = []) |
||
381 | |||
382 | /** |
||
383 | * Renders a static string by applying a layout. |
||
384 | * @param string $content the static string being rendered |
||
385 | * @return string the rendering result of the layout with the given static string as the `$content` variable. |
||
386 | * If the layout is disabled, the string will be returned back. |
||
387 | * @since 2.0.1 |
||
388 | */ |
||
389 | public function renderContent($content) |
||
398 | |||
399 | /** |
||
400 | * Renders a view without applying layout. |
||
401 | * This method differs from [[render()]] in that it does not apply any layout. |
||
402 | * @param string $view the view name. Please refer to [[render()]] on how to specify a view name. |
||
403 | * @param array $params the parameters (name-value pairs) that should be made available in the view. |
||
404 | * @return string the rendering result. |
||
405 | * @throws InvalidParamException if the view file does not exist. |
||
406 | */ |
||
407 | public function renderPartial($view, $params = []) |
||
411 | |||
412 | /** |
||
413 | * Renders a view file. |
||
414 | * @param string $file the view file to be rendered. This can be either a file path or a path alias. |
||
415 | * @param array $params the parameters (name-value pairs) that should be made available in the view. |
||
416 | * @return string the rendering result. |
||
417 | * @throws InvalidParamException if the view file does not exist. |
||
418 | */ |
||
419 | 7 | public function renderFile($file, $params = []) |
|
423 | |||
424 | /** |
||
425 | * Returns the view object that can be used to render views or view files. |
||
426 | * The [[render()]], [[renderPartial()]] and [[renderFile()]] methods will use |
||
427 | * this view object to implement the actual view rendering. |
||
428 | * If not set, it will default to the "view" application component. |
||
429 | * @return View|\yii\web\View the view object that can be used to render views or view files. |
||
430 | */ |
||
431 | 7 | public function getView() |
|
438 | |||
439 | /** |
||
440 | * Sets the view object to be used by this controller. |
||
441 | * @param View|\yii\web\View $view the view object that can be used to render views or view files. |
||
442 | */ |
||
443 | public function setView($view) |
||
447 | |||
448 | /** |
||
449 | * Returns the directory containing view files for this controller. |
||
450 | * The default implementation returns the directory named as controller [[id]] under the [[module]]'s |
||
451 | * [[viewPath]] directory. |
||
452 | * @return string the directory containing the view files for this controller. |
||
453 | */ |
||
454 | public function getViewPath() |
||
461 | |||
462 | /** |
||
463 | * Sets the directory that contains the view files. |
||
464 | * @param string $path the root directory of view files. |
||
465 | * @throws InvalidParamException if the directory is invalid |
||
466 | * @since 2.0.7 |
||
467 | */ |
||
468 | public function setViewPath($path) |
||
472 | |||
473 | /** |
||
474 | * Finds the applicable layout file. |
||
475 | * @param View $view the view object to render the layout file. |
||
476 | * @return string|boolean the layout file path, or false if layout is not needed. |
||
477 | * Please refer to [[render()]] on how to specify this parameter. |
||
478 | * @throws InvalidParamException if an invalid path alias is used to specify the layout. |
||
479 | */ |
||
480 | public function findLayoutFile($view) |
||
516 | } |
||
517 |