top-think / think-migration

thinkphp 数据库迁移工具
Other
91 stars 57 forks source link

Fatal error: Uncaught Error: Class 'think\Console' not found #41

Open sorry510 opened 4 years ago

sorry510 commented 4 years ago

我使用的tp版本为5.1think-migration版本为2.0.3,这个库本身使用没有问题,但是会导致其它库的脚本无法执行,我使用这个库(captainhook/captainhook)时,此库需要使用自动加载加载文件,执行cli的脚本,但是当require vendor/autoload.php时 库的源码如下

 // check installation type [composer bin dir, git clone / phar, composer package dir]
    $composerAutoloadLocations = [
        __DIR__ . '/../autoload.php',
        __DIR__ . '/../vendor/autoload.php',
        __DIR__ . '/../../../autoload.php'
    ];

    foreach ($composerAutoloadLocations as $file) {
        if (file_exists($file)) {
            define('CAPTAINHOOK_COMPOSER_AUTOLOAD', $file);
            break;
        }
    }
    unset($file);

    if (!defined('CAPTAINHOOK_COMPOSER_AUTOLOAD')) {
        fwrite(STDERR,
            'You need to set up the project dependencies using the following commands:' . PHP_EOL .
            'wget http://getcomposer.org/composer.phar' . PHP_EOL .
            'php composer.phar install' . PHP_EOL
        );
        exit(1);
    }

    require CAPTAINHOOK_COMPOSER_AUTOLOAD;

就会报如下错误,不光是这个库,我试了凡是用的自动加载的都会报错,这个应该是think-migration库的问题,我单独试了这个库没有问题,在laravel试了也没有问题

Fatal error: Uncaught Error: Class 'think\Console' not found in E:\UPUPW_NP7.2_64\vhosts\contract_tp5.1\vendor\topthink\think-migration\src\config.php on line 10

config.php的文件源码如下,为什么使用自动加载会无法加载到think\Console这个类呢,希望能修复这个bug


\think\Console::addDefaultCommands([
    "think\\migration\\command\\migrate\\Create",
    "think\\migration\\command\\migrate\\Run",
    "think\\migration\\command\\migrate\\Rollback",
    "think\\migration\\command\\migrate\\Breakpoint",
    "think\\migration\\command\\migrate\\Status",
    "think\\migration\\command\\seed\\Create",
    "think\\migration\\command\\seed\\Run",
]);
pifeifei commented 4 years ago

我也遇到了,问题解决

原因:

解决:

cd /path/to/thinkphp5.1 #或其他 thinkphp 版本目录

# window
mklink /j  vendor\topthink\framework thinkphp
# linux
ln -s thinkphp vendor/topthink/framework
# 运行不再报错
composer dumpautoload

PS:如想要彻底解决需要些脚本自动创建对应软连接

sorry510 commented 4 years ago

我也遇到了,问题解决

原因:

  • composer 不支持 composer.json 中的 extra.think-path 配置
  • 只有 thinkphp 5.1 存在这个问题,5.2, 6.0 不存在这个问

解决:

cd /path/to/thinkphp5.1 #或其他 thinkphp 版本目录

# window
mklink /j  vendor\topthink\framework thinkphp
# linux
ln -s thinkphp vendor/topthink/framework
# 运行不再报错
composer dumpautoload

PS:如想要彻底解决需要些脚本自动创建对应软连接

我用5.1试了还是不行