项目场景:

使用的场景是thinkphp6.0框架开发的HkCms开源内容管理系统,但出现的问题不局限于该系统,在使用composer命令安装其他库包会出现这个Class think\Config does not exist错误。


问题描述

在安装一款新库包时,该库包依赖thinphp,在安装成功后,当再次更新或安装其他插件时会报出以下错误:(Class think\Config does not exist)

PHP Fatal error:  Uncaught ReflectionException: Class think\Config does not exist in D:\hkcms\vendor\topthink\framework\src\think\Container.php:375
Stack trace:
#0 D:\hkcms\vendor\topthink\framework\src\think\Container.php(375): ReflectionClass->__construct('think\\Config')
#1 D:\hkcms\vendor\topthink\framework\src\think\Container.php(251): think\Container->invokeClass('think\\Config', Array)
#2 D:\hkcms\vendor\topthink\framework\src\think\Container.php(130): think\Container->make('think\\Config')
#3 D:\hkcms\vendor\topthink\framework\src\think\Container.php(510): think\Container->get('config')
#4 D:\hkcms\vendor\topthink\framework\src\think\App.php(542): think\Container->__get('config')
#5 D:\hkcms\vendor\topthink\framework\src\think\App.php(451): think\App->load()
#6 D:\hkcms\vendor\topthink\framework\src\think\Console.php(110): think\App->initialize()
#7 D:\hkcms\v in D:\hkcms\vendor\topthink\framework\src\think\Container.php on line 377
Script @php think service:discover handling the post-autoload-dump event returned with error code 255

原因分析:

后来测试发现composer里面如果是依赖thinkphp库包,加载顺序是在thinkphp前面,那么在安装时就会出现这个情况,但是如果库包的文件名排序在thinkphp后面就没什么问题。


解决方案:

通过分析以上情况,可能就是加载顺序问题,而composer中有个自动加载优化器的设置,通过

在composer配置中设置 optimize-autoloader:true  开启自动加载优化器后,就能解决这个问题。

设置方法:

去到项目根目录下打开composer.json文件,写入以下配置即可开启。

"config": {
    "preferred-install": "dist",
    "optimize-autoloader": true
}

提示:hkcms新版本已经加上了这个自动加载优化器代码,不会出现这个情况了。 .

如果你是使用thinkphp,也出现这个错误,不妨试试开启自动加载优化器,看是否能解决这个问题。

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐