程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> yii2源碼學習筆記(十一),yii2源碼學習筆記

yii2源碼學習筆記(十一),yii2源碼學習筆記

編輯:關於PHP編程

yii2源碼學習筆記(十一),yii2源碼學習筆記


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 <[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      * 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 corresponding
100      * 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 action
114      * 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             // 記錄當前的操作為requestedAction
145             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的beforeAction
155         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 執行成功就執行action
170             $result = $action->runWithParams($params);
171             // 執行controller本身的afterAction
172             $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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved