Yii2.0 執行流程分析

 1 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\ClassLoader
12     ---->引入 autoload_namespaces.php 給做爲屬性 $loader
13         ---->$vendorDir  $baseDir
14     ---->引入 autoload_psr4.php 做爲屬性給 $loader
15     ---->$loader->register(true);
16         ---->spl_autoload_register this,loadClass
17         ---->loadClass ----> findFile 
18     ---->引入 autoload_files.php require
19     ---->return $loader
20 index.php
21     ---->初始化了一個$loader  (暫時不知道什麼用)
22     ---->引入 /vendor/yiisoft/yii2/Yii.php
23 Yii.php
24     ----> 引入 BaseYii.php ,Yii 繼承 BaseYii
25     ---->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 -> Object
32  
33 index.php
34     ---->$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 security
46         ---->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.php
55             ---->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]handler
60             ---->$event->sender = application object
61                  $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/request
70                 ---->list ($route, $params) = $request->resolve();//解決當前請求的路由和相關參數  
71                     ---->$params 放置地址欄解析的結果數組Array ( [0] => [1] => Array ( [m] => sds [c] => dasd ) )
72                     ---->runAction($route, $params); //位於Module
73                         ---->list($controller, $actionID) = $this->createController($route) 返回array('0'=>控制器controller對象,'1'=>'action名') 
74                         $controller 賦給Yii::$app->controller
75                         ---->$controller->runAction($actionID, $params);  yii/base/Controller
76  
77                         ---->runAction($actionID, $params);  yii/base/Controller
78                             ---->$action = $this->createAction($id); //生成一個InlineAction對象,賦給Yii::$app->requestedAction
79                                 InlineAction __construct $this->actionMethod = $actionMethod;
80                             ---->beforeAction
81                             ---->$action->runWithParams($params); //位於 yii/base/InlineAction
82                                 ---->$args = $this->controller->bindActionParams($this, $params);//位於yii/web/controller $this=>InlineAction $params=>模塊/控制器 數組  --- 將參數綁定到action,返回有效參數數組$args
83                                 ---->賦給Yii::$app->requestedParams = $args;
84                                 ---->call_user_func_array([$this->controller, $this->actionMethod], $args) //執行第一個回調函數 真正執行
85                             ---->afterAction
86                             ---->返回執行結果(頁面已出) 給Module中的runAction
87  
88         ---->返回結果給handleRequest
89         ---->$response = $this->getResponse(); 返回一個response對象,具體同上
90         ---->$response->data = $result;  
91         ---->返回$response給yii/base/Application 的 run $response
92         ---->$response->send();輸出內容
93         <!-- 頁面輸出完成 -->
94  
95  
96  
相關文章
相關標籤/搜索