Yii2 source code learning notes (11), yii2 source code learning notes _ PHP Tutorial

Source: Internet
Author: User
Yii2 source code learning notes (11) and yii2 source code learning notes. Yii2 source code learning note (11), yii2 source code learning note Controller class, is the base class of all controllers for calling models and layout. 1? Php2 ** 3 * @ linkwww. yii2 source code learning notes (11), yii2 source code learning Notes

The Controller class is the base class of all controllers for calling models and layout.

1
 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 * the ActionEvent event is executed before the correct executor action is proposed. 34 * You may set [[ActionEvent: isValid] to be false to cancel the action execution. 35 * If the isValid attribute of the event is set to false, the action will be canceled for 36 */37 const EVENT_BEFORE_ACTION = 'beforeaction '; 38/** 39 * @ event ActionEvent an event raised right after executing a controller action. 40 * events triggered after the controller operation are executed 41 */42 const EVENT_AFTER_ACTION = 'afteraction'; 43 44/** 45 * @ var string the ID of this controlle R. 46 * controller id 47 */48 public $ id; 49/** 50 * @ var Module $ module the module that this controller belongs. 51 * module 52 */53 public $ module; 54/** 55 * @ var string the ID of the action that is used when the action ID is not specified 56 * in the request. ULTS to 'index '. the default action in the controller. the default value is index 57 */58 public $ defaaction action = 'index'; 59/** 60 * @ var string | boolean the name of the layout to B. E applied to this controller's views. 61 * the layout name is applied to the controller's view. 62 * This property mainly affects the behavior of [[render ()]. this attribute mainly affects [[render ()] behavior 63 * Defaults to null, meaning the actual layout value shoshould inherit that from [module]'s layout value. 64 * If false, no layout will be applied. 65 * if set to false, the layout file 66 */67 public $ layout; 68/** 69 * @ var Action the action that is currently being executed is not used. this property will be set 70 * by [[run ()] whe N it is called by [[Application] to run an action. 71 * currently executed operations. you can perform different operations in the event according to this action 72 */73 public $ action; 74 75/** 76 * @ var View the view object that can be used to render views or view files. 77 * view object, used to define the output view file 78 */79 private $ _ view; 80 81 82/** 83 * @ param string $ id the ID of this controller. controller ID 84 * @ param Module $ module the module that this controller belongs. controller Module 85 * @ Param array $ config name-value pairs that will be used to initialize the object properties. 86 * configuration file 87 */88 public function _ construct ($ id, $ module, $ config = []) 89 {90 // Initialization controller id, module, initialize the controller object 91 $ this-> id = $ id; 92 $ this-> module = $ module; 93 parent ::__ construct ($ config) based on the configuration file ); 94} 95 96/** 97 * Declares external actions for the controller. define action declaration controller's external operation 98 * This method is me Ant to be overwritten to declare external actions for the controller. 99 * It shoshould return an array, with array keys being action IDs, and array values the corresponding100 * action class names or action configuration arrays. for example, 101 * this method specifies an independent action. The Returned format is an array, the name is the id of the action, and the value is the implementation of the action Class. 102 *~~~ 103 * return [104 * 'action1' => 'app \ components \ action1 ', 105 * 'action2' => [106 * 'class' => 'app \ components \ action2', 107 * 'property1' => 'value1 ', 108 * 'property2' => 'value2', 109 *], 110 *]; 111 *~~~ 112*113 * [[\ Yii: createObject ()] will be used later to create the requested action114 * using the configuration provided here.115 * use the configuration provided here to create request operations. 116 */117 public function actions () 118 {119 return []; 120} 121 122/** 123 * Runs an action within this controller with the specified action ID and parameters.124 * run the specified operation ID and parameters in the controller. 125 * If the action ID is empty, the method will use [[defaultAction]. 126 * if no ID is defined, the default operation 127 * @ param string $ id the ID of the action to be executed will be called. ID of the action to be executed. 128 * @ param array $ params the parameters (name-value pairs) to be passed to the action.129 * parameter passed to the operation. 130 * @ return mixed the result of the action. operation result 131 * @ throws InvalidRouteException if the requested action ID cannot be resolved into an action successfully.132 * @ see createAction () 133 */134 public function runAction ($ id, $ params = []) 135 {136 $ action = $ this-> createAction ($ id); // Create operation 137 if ($ action = null) {// creation failed, throwing an exception 138 throw new InvalidRouteException ('unable to resolve the request :'. $ this-> getUniqueId (). '/'. $ id); 139} 140 141 Yii: trace ("Route to run :". $ action-> getUniqueId (), _ METHOD _); 142 143 if (Yii: $ app-> requestedAction = null) {144 // record the current operation as requestedAction145 Yii ::$ app-> requestedAction = $ action; 146} 147 $ oldAction = $ this-> action; // save the information in the operation 149 $ this-> action = $ action; // write property 150 // save all parent modules of the current controller 151 $ modules = []; 152 $ runAction = true; 153 154 // call beforeAction on modules layer-by-layer execution of module beforeAction155 foreach ($ this-> getModules () as $ module) {156 if ($ module-> beforeAction ($ action) {157 // put the successfully executed module into $ modules. The order is reversed by 158 array_unshift ($ modules, $ module); 159} else {160 // if the execution fails, Mark 161 $ runAction = false; 162 break; 163} 164} 165 166 $ result = null; 167 168 if ($ runAction & $ this-> beforeAction ($ action )) {169 // run the action to execute action170 $ result = $ action-> runWithParams ($ params ); 171 // execute afterAction172 of the controller itself $ result = $ this-> afterAction ($ action, $ result ); 173 174 // call afterAction on modules execute all 175 foreach layers from the inside out ($ modules as $ module) {176/* @ var $ module Module */177 $ result = $ module-> afterAction ($ action, $ result ); 178} 179} 180 181 $ this-> action = $ oldAction; 182 183 return $ result; 184}

Yii2 \ base \ Controller. php

Objective (11), yii2 source code learning note Controller class, is the base class of all controllers, used to call models and layout. 1? Php 2/** 3 * @ link http: // www ....

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.