ThinkPHP命令行运行报错'think\Console' not found

环境
  • Thinkphp5.1
命令行执行
php s1.php

错误提示:

PHP Fatal error:  Uncaught Error: Class 'think\Console' not found in /www/***/vendor/topthink/think-worker/src/command.php:12
Stack trace:
#0 /www/***/vendor/composer/autoload_real.php(66): require()
#1 /www/***/vendor/composer/autoload_real.php(56): composerRequire29d161512c2a2356c427813fcdcc5018('ffc1d7141d4fcba...', '/www/***...')
#2 /www/***/vendor/autoload.php(7): ComposerAutoloaderInit29d161512c2a2356c427813fcdcc5018::getLoader()
#3 /www/***/s1.php(26): require_once('/www/***...')
#4 {main}
  thrown in /www/***/vendor/topthink/think-worker/src/command.php on line 12
代码 s1.php
// 标记是全局启动
define('GLOBAL_START', 1);

require_once __DIR__ . '/vendor/autoload.php';

// 加载所有Applications/*/start.php,以便启动所有服务
// foreach (glob(__DIR__ . '/ws/*/start*.php') as $start_file) {
//     require_once $start_file;
// }
// 运行所有服务
Worker::runAll();
我的疑问

1.为什么他会报错 'think\Console' not found,我并没有调用think相关代码呀?

阅读 12.2k
5 个回答

因为你autoload

新手上路,请多包涵

有2种方法解决:
1.在引入autoload.php 之前引入一下 核心框架下面的Console.php
比如:

require_once __DIR__ . '/thinkphp/library/think/Console.php';
require_once __DIR__ . '/vendor/autoload.php';

2.把vendor/topthink/think-worker/src/command 下的 注释 掉

\think\Console::addDefaultCommands([
    'worker:gateway' => '\\think\\worker\\command\\GatewayWorker',
    'worker:server'  => '\\think\\worker\\command\\Server',
    'worker'         => '\\think\\worker\\command\\Worker',
]);

然后在自己的应用里面的command.php 把下面这段复制进去数组

    'worker:gateway' => '\\think\\worker\\command\\GatewayWorker',
    'worker:server'  => '\\think\\worker\\command\\Server',
    'worker'         => '\\think\\worker\\command\\Worker',
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题