controller控制器类,是所有控制器的基类,用于调用模型和布局。
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 * @property module[] $modules all ancestor modules that this controller is located within. this property is 16 * read-only.只读属性 当前控制器的所有模块 17 * @property string $route the route (module id, controller id and action id) of the current request. this 18 * property is read-only.当前请求的路径 只读属性 可以获取到请求的路径 19 * @property string $uniqueid the controller id that is prefixed with the module id (if any). this property is 20 * read-only.为前缀的controller id 唯一标识 21 * @property view|\yii\web\view $view the view object that can be used to render views or view files. 22 * 视图用来传递视图或视图文件. 23 * @property string $viewpath the directory containing the view files for this controller. this property is 24 * read-only. 包含当前控制器的视图目录 25 * 26 * @author qiang xue 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 * actionevent事件提出正确的执行器动作之前执行。 34 * you may set [[actionevent::isvalid]] to be false to cancel the action execution. 35 * 如果对事件的isvalid属性设置为false,将取消action的执行 36 */ 37 const event_before_action = 'beforeaction'; 38 /** 39 * @event actionevent an event raised right after executing a controller action. 40 * 在执行controller操作后触发的事件 41 */ 42 const event_after_action = 'afteraction'; 43 44 /** 45 * @var string the id of this controller. 46 * 控制器id 47 */ 48 public $id; 49 /** 50 * @var module $module the module that this controller belongs to. 51 * 所属模块 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. defaults to 'index'.控制器中默认动作,默认为index 57 */ 58 public $defaultaction = 'index'; 59 /** 60 * @var string|boolean the name of the layout to be applied to this controller's views. 61 * 布局的名称 应用到该控制器的视图。 62 * this property mainly affects the behavior of [[render()]].此属性主要影响[[render()]]行为 63 * defaults to null, meaning the actual layout value should inherit that from [[module]]'s layout value. 64 * if false, no layout will be applied. 65 * 如果设置为false,则不使用布局文件 66 */ 67 public $layout; 68 /** 69 * @var action the action that is currently being executed. this property will be set 70 * by [[run()]] when it is called by [[application]] to run an action. 71 * 当前执行的操作,可在事件中根据这个action来执行不同的操作 72 */ 73 public $action; 74 75 /** 76 * @var view the view object that can be used to render views or view files. 77 * 视图对象,用来定义输出的视图文件 78 */ 79 private $_view; 80 81 82 /** 83 * @param string $id the id of this controller.控制器的id 84 * @param module $module the module that this controller belongs to.控制器的模块 85 * @param array $config name-value pairs that will be used to initialize the object properties. 86 * 初始化对像时的配置文件 87 */ 88 public function __construct($id, $module, $config = []) 89 { 90 //初始化控制器id,模块,根据配置文件初始化控制器对象 91 $this->id = $id; 92 $this->module = $module; 93 parent::__construct($config); 94 } 95 96 /** 97 * declares external actions for the controller.定义action声明控制器的外部操作 98 * this method is meant to be overwritten to declare external actions for the controller. 99 * it should return an array, with array keys being action ids, and array values the corresponding100 * action class names or action configuration arrays. for example,101 * 这个方法指定独立的action,返回格式为数组,name为action的id,value为action类的实现,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 * 使用此处提供的配置来创建请求的操作。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 * 控制器中运行指定的操作标识和参数。125 * if the action id is empty, the method will use [[defaultaction]].126 * 如果没有定义id,会调用默认操作127 * @param string $id the id of the action to be executed. 要执行的动作标识。128 * @param array $params the parameters (name-value pairs) to be passed to the action.129 * 传递给操作的参数。130 * @return mixed the result of the action. 操作结果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);//创建操作137 if ($action === null) {//创建失败,抛出异常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 // 记录当前的操作为requestedaction145 yii::$app->requestedaction = $action;146 }147 148 $oldaction = $this->action;//将操作中的信息保存149 $this->action = $action;//写入属性150 //保存当前控制器的所有父模块151 $modules = [];152 $runaction = true;153 154 // call beforeaction on modules 从外到里一层层执行module的beforeaction155 foreach ($this->getmodules() as $module) {156 if ($module->beforeaction($action)) {157 // 将执行成功的module放入到$modules中,顺序会颠倒158 array_unshift($modules, $module);159 } else {160 // 执行失败,就标记一下161 $runaction = false;162 break;163 }164 }165 166 $result = null;167 168 if ($runaction && $this->beforeaction($action)) {169 // run the action 执行成功就执行action170 $result = $action->runwithparams($params);171 // 执行controller本身的afteraction172 $result = $this->afteraction($action, $result);173 174 // call afteraction on modules 从里到外一层层执行所有175 foreach ($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