本文主要和大家分享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\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
以上就是yii2.0执行流程详解的详细内容。