标签:
控制器类,所有控制器的基类,用于调用模型和布局,输出到视图
1 namespace yii\base; 2 3 use Yii; 4 5 /** 6 * Controller is the base class for classes containing controller logic. 7 * 控制器,是所用控制器类的基类 8 * 9 * @property Module[] $modules 只读属性 当前控制器的所有模块 10 * 11 * @property string $route (module ID, controller ID and action ID) 当前请求的路径 只读属性 可以获取到请求的路径 12 * 13 * @property string $uniqueId 以module ID(如果有的话) 为前缀的controller ID 应该是唯一标识的作用 14 * 15 * @property View|\yii\web\View $view 视图 object 用来传递视图或视图文件. 16 * 17 * @property string $viewPath 包含当前控制器的视图目录. 18 * 19 * @author Qiang Xue <qiang.xue@gmail.com> 20 * @since 2.0 21 */ 22 class Controller extends Component implements ViewContextInterface 23 { 24 /** 25 * @event ActionEvent an event raised right before executing a controller action. 26 * You may set [[ActionEvent::isValid]] to be false to cancel the action execution. 27 * 在执行beforeAction方法时触发的事件,如果对事件的isValid属性设置为false,将取消action的执行 28 */ 29 const EVENT_BEFORE_ACTION = ‘beforeAction‘; 30 /** 31 * @event ActionEvent an event raised right after executing a controller action. 32 * 在执行afterAction方法是触发的事件 33 */ 34 const EVENT_AFTER_ACTION = ‘afterAction‘; 35 36 /** 37 * @var string the ID of this controller. 38 * 控制器id, 39 */ 40 public $id; 41 /** 42 * @var Module $module the module that this controller belongs to. 43 * 所属模块 44 */ 45 public $module; 46 /** 47 * @var string the ID of the action that is used when the action ID is not specified 48 * in the request. Defaults to ‘index‘. 49 * 控制器中默认动作,默认为index 50 */ 51 public $defaultAction = ‘index‘; 52 /** 53 * @var string|boolean 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 * 布局文件,如果设置为false,则不使用布局文件 58 */ 59 public $layout; 60 /** 61 * @var Action the action that is currently being executed. This property will be set 62 * by [[run()]] when it is called by [[Application]] to run an action. 63 * 当前下面执行的action,可在事件中根据这个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 */ 71 private $_view; 72 /** 73 * @var string the root directory that contains view files for this controller. 74 */ 75 private $_viewPath; 76 77 78 /** 79 * @param string $id 当前控制器的ID 80 * @param Module $module 当前控制器的模块 81 * @param array $config 初始化对像时的配置文件 82 */ 83 public function __construct($id, $module, $config = []) 84 { 85 //初始化指定控制器id,模块,根据配置文件初始化控制器对象 86 $this->id = $id; 87 $this->module = $module; 88 parent::__construct($config); 89 } 90 91 /** 92 * Declares external actions for the controller. 93 * 定义action 声明控制器的外部操作 94 * This method is meant to be overwritten to declare external actions for the controller. 95 * It should return an array, with array keys being action IDs, and array values the corresponding 96 * action class names or action configuration arrays. For example, 97 * 这个用来指定独立的action,返回格式为name-value的数组,name为action的id,value为action类的实现,如: 98 * 99 * ```php 100 * return [ 101 * ‘action1‘ => ‘app\components\Action1‘, 102 * ‘action2‘ => [ 103 * ‘class‘ => ‘app\components\Action2‘, 104 * ‘property1‘ => ‘value1‘, 105 * ‘property2‘ => ‘value2‘, 106 * ], 107 * ]; 108 * ``` 109 * 110 * [[\Yii::createObject()]] will be used later to create the requested action 111 * using the configuration provided here. 112 * 这个主要是用于在子类中重写 113 */ 114 115 public function actions() 116 { 117 return []; 118 } 119 120 /** 121 * Runs an action within this controller with the specified action ID and parameters. 122 * If the action ID is empty, the method will use [[defaultAction]]. 123 * $id 为action的id,也就是操作的名称,如定义的actionIndex,那么id就为Index。 124 * 如果没有定义 action ID,就会调用默认的操作,例如常用的index 125 * 126 * @param string $id 操作id,也就是操作名 127 * @param array $params the parameters (name-value pairs) to be passed to the action. 128 * @return mixed the result of the action. 129 * @throws InvalidRouteException if the requested action ID cannot be resolved into an action successfully. 130 * @see createAction() 131 */ 132 public function runAction($id, $params = []) 133 { 134 //创建action 135 $action = $this->createAction($id); 136 if ($action === null) { 137 //创建action失败,抛出异常 138 throw new InvalidRouteException(‘Unable to resolve the request: ‘ . $this->getUniqueId() . ‘/‘ . $id); 139 } 140 //写入trace信息 141 Yii::trace(‘Route to run: ‘ . $action->getUniqueId(), __METHOD__); 142 143 if (Yii::$app->requestedAction === null) { 144 Yii::$app->requestedAction = $action;//不知道这个是干嘛用的0.0 145 } 146 147 $oldAction = $this->action;//将action中的信息保存到oldAction 148 $this->action = $action; //将当前的action写入action属性中 149 //用来保存当前控制器的所有父模块,顺序为由子模块到父模块 150 $modules = []; 151 $runAction = true; 152 153 /* 154 * 获取当前控制器的所有的模块,并执行每个模块的beforeAction来检查当前的action是否可以执行, 155 * 注意:getModules返回的数组顺序为:从父模块到子模块, 156 * 所以在执行beforeAction的时候,先检查最外层的父模块,然后检查子模块。 157 * 158 * 然而在执行afterAction的时候,顺序就反过来了,先执行子模块,最后执行父模块。 159 * 160 */ 161 foreach ($this->getModules() as $module) { 162 if ($module->beforeAction($action)) { 163 array_unshift($modules, $module); 164 } else { 165 $runAction = false; 166 break; 167 } 168 } 169 170 $result = null; 171 //如果所有的父模块都满足执行的条件 172 if ($runAction && $this->beforeAction($action)) {//判断当前控制器中beforeAction 173 // 由生成的action对象来执行runWithParams方法 174 $result = $action->runWithParams($params); 175 //执行完后,再执行afterAction方法 176 $result = $this->afterAction($action, $result); 177 178 //执行所有父模块的afterAction 179 foreach ($modules as $module) { 180 /* @var $module Module */ 181 $result = $module->afterAction($action, $result); 182 } 183 } 184 $this->action = $oldAction;//有什么用呢?,看完后面的在回头看吧 185 186 return $result; 187 }
标签:
原文地址:http://www.cnblogs.com/isleep/p/5406605.html