index.php
->引入 vendor/auto_load.php auto_load.php ->引入 ventor/composer/autoload_real.php ->执行 ComposerAutoloaderInit240f916b39e20bc11bc03e2039805bd4->getLoader autoload_real.php ->getLoader ->单例 ->spl_autoload_register(array('ComposerAutoloaderInit240f916b39e20bc11bc03e2039805bd4','loadClassLoader')) ->self::$loader = new \Composer\Autoload\ClassLoader(); ->引入 \Composer\Autoload\ClassLoader ->引入 autoload_namespaces.php 给作为属性 $loader ->$vendorDir $baseDir ->引入 autoload_psr4.php 作为属性给 $loader ->$loader->register(true); ->spl_autoload_register this,loadClass ->loadClass -> findFile ->引入 autoload_files.php require ->return $loader index.php ->初始化了一个$loader (暂时不知道什么用) ->引入 /vendor/yiisoft/yii2/Yii.php Yii.php -> 引入 BaseYii.php ,Yii 继承 BaseYii ->spl_autoload_register(BaseYii,autoload) ->Yii::$classMap = include(__DIR__ . '/classes.php'); //引入一堆php class地址 ->Yii::$container = new yii\di\Container;//容器 //继承关系梳理 yii\di\Container(容器) -> yii\base\Component(实现 属性,事件,行为 功能的基类) -> Object(实现 属性 功能的基类,含有__construct) yii\web\Application(所有web应用类的基类) -> \yii\base\Application(所有应用的基类,__construct) -> Module(所有模块和应用的基类,含有__construct) -> yii\di\ServiceLocator(服务定位器,包含所有模块和应用) -> yii\base\Component -> Object
index.php
->$config 引入 (new yii\web\Application($config))->run(); ->\yii\base\Application __construct() ->Yii::$app = $this (Application) ->$this->setInstance($this); 设置当前请求类的实例 (把Application类的对象push进Yii的loadedModules里) ->$this->preInit($config); ->$this->_basePath = $_config['basepath'] Yii::aliases[@app] = $_config['basepath'] ->$this->getVendorPath 设置框架路径 ->setVendorPath Yii::aliases[@vendor] Yii::aliases[@bower] Yii::aliases[@npm] ->$this->getRuntimePath 同上,设置runtimePath Yii::aliases[@runtime] ->setTimeZone 设置时区 ->核心组件信息(地址)注入$config log view formatter i18n mailer urlManager assetManager security ->registerErrorHandler 定义错误处理程序 ->Component::__construct($config); Object中的__construct //这步发生了很多事情 ->Yii::configure($this) 把$config赋给$this作属性 ? $this->bootstrap 中的值哪来的 ?->配置文件来的。。。。 ->$this->init() ->$this->bootstrap(); 初始化扩展和执行引导组件。 ->引入@vendor/yiisoft/extensions.php ->Yii::aliases['xxx'] = 'xxx'; extensions.php中aliase地址 <!-- 初始化完成 --> ->\yii\base\Application->run() ->$this->trigger($name) $event = new Event; //$name = beforeRequest 执行 _event[beforeRequest]handler ->$event->sender = application object $event->name = $name; //这两句没懂 $event->data = $handler[1]; call_user_func($handler[0], $event); Event::trigger($this, $name, $event); //$this = application object ->$response = $this->handleRequest($this->getRequest()); ->$this->getRequest() ->get('request') get方法位于ServiceLocator ,返回指定id的实例(返回request实例到_components['request']) ->$this->handleRequest(request对象) //request对象的类是yii/web/request ->list ($route, $params) = $request->resolve();//解决当前请求的路由和相关参数 ->$params 放置地址栏解析的结果数组Array ( [0] => [1] => Array ( [m] => sds 1 => dasd ) ) ->runAction($route, $params); //位于Module ->list($controller, $actionID) = $this->createController($route) 返回array('0'=>控制器controller对象,'1'=>'action名') $controller 赋给Yii::$app->controller ->$controller->runAction($actionID, $params); yii/base/Controller ->runAction($actionID, $params); yii/base/Controller ->$action = $this->createAction($id); //生成一个InlineAction对象,赋给Yii::$app->requestedAction InlineAction __construct $this->actionMethod = $actionMethod; ->beforeAction ->$action->runWithParams($params); //位于 yii/base/InlineAction ->$args = $this->controller->bindActionParams($this, $params);//位于yii/web/controller $this=>InlineAction $params=>模块/控制器 数组 将参数綁定到action,返回有效参数数组$args ->赋给Yii::$app->requestedParams = $args; ->call_user_func_array([$this->controller, $this->actionMethod], $args) //执行第一个回调函数 真正执行 ->afterAction ->返回执行结果(页面已出) 给Module中的runAction ->返回结果给handleRequest ->$response = $this->getResponse(); 返回一个response对象,具体同上 ->$response->data = $result; ->返回$response给yii/base/Application 的 run $response ->$response->send();输出内容 <!-- 页面输出完成 -->
转载请注明:(●--●) Hello.My Weicot » Yii2.0 执行行流程分析