本文主要和大家分享Yii2.0執行流程詳解,主要以代碼的形式和大家分享,希望能協助到大家。
index.php 2 ---->引入 vendor/auto_load.php 3 auto_load.php 4 ---->引入 ventor/composer/autoload_real.php 5 ---->執行 ComposerAutoloaderInit240f916b39e20bc11bc03e2039805bd4->getLoader 6 autoload_real.php 7 ---->getLoader 8 ---->單例 9 ---->spl_autoload_register(array('ComposerAutoloaderInit240f916b39e20bc11bc03e2039805bd4','loadClassLoader'))10 ---->self::$loader = new \Composer\Autoload\ClassLoader();11 ---->引入 \Composer\Autoload\ClassLoader12 ---->引入 autoload_namespaces.php 給作為屬性 $loader13 ---->$vendorDir $baseDir14 ---->引入 autoload_psr4.php 作為屬性給 $loader15 ---->$loader->register(true);16 ---->spl_autoload_register this,loadClass17 ---->loadClass ----> findFile 18 ---->引入 autoload_files.php require19 ---->return $loader20 index.php21 ---->初始化了一個$loader (暫時不知道什麼用)22 ---->引入 /vendor/yiisoft/yii2/Yii.php23 Yii.php24 ----> 引入 BaseYii.php ,Yii 繼承 BaseYii25 ---->spl_autoload_register(BaseYii,autoload)26 ---->Yii::$classMap = include(__DIR__ . '/classes.php'); //引入一堆php class地址27 ---->Yii::$container = new yii\di\Container;//容器28 29 //繼承關係梳理30 yii\di\Container(容器) -> yii\base\Component(實現 屬性,事件,行為 功能的基類) -> Object(實現 屬性 功能的基類,含有__construct)31 yii\web\Application(所有web應用類的基類) -> \yii\base\Application(所有應用的基類,__construct) -> Module(所有模組和應用的基類,含有__construct) -> yii\di\ServiceLocator(服務定位器,包含所有模組和應用) -> yii\base\Component -> Object32 33 index.php34 ---->$config 引入 35 (new yii\web\Application($config))->run();36 ---->\yii\base\Application __construct()37 ---->Yii::$app = $this (Application)38 ---->$this->setInstance($this); 設定當前請求類的執行個體 (把Application類的對象push進Yii的loadedModules裡)39 ---->$this->preInit($config);40 ---->$this->_basePath = $_config['basepath'] Yii::aliases[@app] = $_config['basepath']41 ---->$this->getVendorPath 設定架構路徑42 ---->setVendorPath Yii::aliases[@vendor] Yii::aliases[@bower] Yii::aliases[@npm]43 ---->$this->getRuntimePath 同上,設定runtimePath Yii::aliases[@runtime]44 ---->setTimeZone 設定時區45 ---->核心組件資訊(地址)注入$config log view formatter i18n mailer urlManager assetManager security46 ---->registerErrorHandler 定義錯誤處理程式47 ---->Component::__construct($config); Object中的__construct //這步發生了很多事情48 ---->Yii::configure($this) 把$config賦給$this作屬性49 50 ? $this->bootstrap 中的值哪來的 ?---->設定檔來的。。。。51 52 ---->$this->init()53 ---->$this->bootstrap(); 初始化擴充和執行引導組件。54 ---->引入@vendor/yiisoft/extensions.php55 ---->Yii::aliases['xxx'] = 'xxx'; extensions.php中aliase地址56 <!-- 初始化完成 -->57 58 ---->\yii\base\Application->run()59 ---->$this->trigger($name) --- $event = new Event; //$name = beforeRequest 執行 _event[beforeRequest]handler60 ---->$event->sender = application object61 $event->name = $name;62 //這兩句沒懂63 $event->data = $handler[1];64 call_user_func($handler[0], $event);65 Event::trigger($this, $name, $event); //$this = application object 66 67 ---->$response = $this->handleRequest($this->getRequest());68 ---->$this->getRequest() ---->get('request') get方法位於ServiceLocator ,返回指定id的執行個體(返回request執行個體到_components['request'])69 ---->$this->handleRequest(request對象) //request對象的類是yii/web/request70 ---->list ($route, $params) = $request->resolve();//解決當前請求的路由和相關參數 71 ---->$params 放置地址欄解析的結果數組Array ( [0] => [1] => Array ( [m] => sds [c] => dasd ) )72 ---->runAction($route, $params); //位於Module73 ---->list($controller, $actionID) = $this->createController($route) 返回array('0'=>控制器controller對象,'1'=>'action名') 74 $controller 賦給Yii::$app->controller75 ---->$controller->runAction($actionID, $params); yii/base/Controller76 77 ---->runAction($actionID, $params); yii/base/Controller78 ---->$action = $this->createAction($id); //產生一個InlineAction對象,賦給Yii::$app->requestedAction79 InlineAction __construct $this->actionMethod = $actionMethod;80 ---->beforeAction81 ---->$action->runWithParams($params); //位於 yii/base/InlineAction82 ---->$args = $this->controller->bindActionParams($this, $params);//位於yii/web/controller $this=>InlineAction $params=>模組/控制器 數組 --- 將參數綁定到action,返回有效參數數組$args83 ---->賦給Yii::$app->requestedParams = $args;84 ---->call_user_func_array([$this->controller, $this->actionMethod], $args) //執行第一個回呼函數 真正執行85 ---->afterAction86 ---->返回執行結果(頁面已出) 給Module中的runAction87 88 ---->返回結果給handleRequest89 ---->$response = $this->getResponse(); 返回一個response對象,具體同上90 ---->$response->data = $result; 91 ---->返回$response給yii/base/Application 的 run $response92 ---->$response->send();輸出內容93 <!-- 頁面輸出完成 -->94 95 96