HubQin
- HubQin 2019-01-16 已签到连续签到5天,获得了20个金钱
- HubQin 2019-01-15 已签到连续签到4天,获得了20个金钱
- HubQin 2019-01-14 已签到连续签到3天,获得了15个金钱
- HubQin 2019-01-13 已签到连续签到2天,获得了10个金钱
- HubQin 2019-01-12 已签到连续签到1天,获得了5个金钱
- HubQin 收藏了教程Yii2框架底层分析,服务定位器
- HubQin 赞了评论
比较重要的漏点就是:初始化的引导过程bootstrap
另外一个就是controllerMap过程和actionMap过程这里我想说的是bootstrap过程。我顺着你的代码说:
1.public function __construct($config = []) { Yii::$app = $this; $this->setInstance($this); $this->state = self::STATE_BEGIN; $this->preInit($config); $this->registerErrorHandler($config); Component::__construct($config); }
上面的代码:Component::construct($config); 最终会执行Object的construct($config)。
Object代码你贴上去了:public function __construct($config = []) { if (!empty($config)) { Yii::configure($this, $config);//将配置文件里面的所有配置信息赋值给Object,由于Object是大部分类的基类,实际上也就是交给了yii\web\Application 您可以Yii::$app->配置参数来访问配置文件中的内容 } $this->init();//下面会细分析 }
需要注意的是代码: $this->init();
现在回到yii\base\Applicationpublic function init() { $this->state = self::STATE_INIT; $this->bootstrap(); }
在这里可以看到执行了bootstrap()方法,这是系统的引导过程,这个 是非常重要的,被你疏漏了。
protected function bootstrap() { if ($this->extensions === null) { $file = Yii::getAlias('@vendor/yiisoft/extensions.php'); $this->extensions = is_file($file) ? include($file) : []; } foreach ($this->extensions as $extension) { if (!empty($extension['alias'])) { foreach ($extension['alias'] as $name => $path) { Yii::setAlias($name, $path); } } if (isset($extension['bootstrap'])) { $component = Yii::createObject($extension['bootstrap']); if ($component instanceof BootstrapInterface) { Yii::trace('Bootstrap with ' . get_class($component) . '::bootstrap()', __METHOD__); $component->bootstrap($this); } else { Yii::trace('Bootstrap with ' . get_class($component), __METHOD__); } } } foreach ($this->bootstrap as $class) { $component = null; if (is_string($class)) { if ($this->has($class)) { $component = $this->get($class); } elseif ($this->hasModule($class)) { $component = $this->getModule($class); } elseif (strpos($class, '\\') === false) { throw new InvalidConfigException("Unknown bootstrapping component ID: $class"); } } if (!isset($component)) { $component = Yii::createObject($class); } if ($component instanceof BootstrapInterface) { Yii::trace('Bootstrap with ' . get_class($component) . '::bootstrap()', __METHOD__); $component->bootstrap($this); } else { Yii::trace('Bootstrap with ' . get_class($component), __METHOD__); } } }
这里包括 yii插件,组件,模块 这三个方面的bootstrap初始化过程。
详细参看文章:http://www.fancyecommerce.com/2016/05/18/yii2-初始化的bootstrap过程-引导/
这里不细说了。
另外,index.php里面的autoload部分,也得细致说一下各个方面的加载
在另外controllerMap actionMap部分也细致说一下,基本就齐全了。 - HubQin 赞了评论
深度好文,yii的行为就类似triat的加强版。最近再看Web Application Development with Yii 2 and PHP 这本书以实践为主的入门书,打算后面有点时间就开始结合自己理解翻译出来给大家看