HubQin

HubQin

Was mich nicht umbringt, macht mich stärker

  • 财富值70
  • 威望值0
  • 总积分90

个人信息

  • 2019-01-16 已签到
    连续签到5天,获得了20个金钱
  • 2019-01-15 已签到
    连续签到4天,获得了20个金钱
  • 2019-01-14 已签到
    连续签到3天,获得了15个金钱
  • 2019-01-13 已签到
    连续签到2天,获得了10个金钱
  • 2019-01-12 已签到
    连续签到1天,获得了5个金钱
  • 收藏了教程
    Yii2框架底层分析,服务定位器
  • 赞了评论

    比较重要的漏点就是:初始化的引导过程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\Application

    public 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部分也细致说一下,基本就齐全了。

  • 赞了评论

    深度好文,yii的行为就类似triat的加强版。最近再看Web Application Development with Yii 2 and PHP 这本书以实践为主的入门书,打算后面有点时间就开始结合自己理解翻译出来给大家看

  • 收藏了教程
    Yii2基本概念之——行为(Behavior)
职场新人 等级规则
90/100
资料完整度
40/100
用户活跃度
20/100

Ta的关注

3

Ta的粉丝

0

Ta的访客

1