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 ComposerAutoloadClassLoader();
11     ---->引入 ComposerAutoloadClassLoader
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 yiidiContainer;//容器
28  
29 //继承关系梳理
30     yiidiContainer(容器) -> yiiaseComponent(实现 属性,事件,行为 功能的基类) -> Object(实现 属性 功能的基类,含有__construct)
31     yiiwebApplication(所有web应用类的基类) -> yiiaseApplication(所有应用的基类,__construct) -> Module(所有模块和应用的基类,含有__construct) -> yiidiServiceLocator(服务定位器,包含所有模块和应用) -> yiiaseComponent -> Object
32  
33 index.php
34     ---->$config 引入 
35     (new yiiwebApplication($config))->run();
36     ---->yiiaseApplication __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     ---->yiiaseApplication->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  
原文地址:https://www.cnblogs.com/cresuccess/p/4874330.html