我正在为Laravel 5.8开发一个软件包。当我尝试创建扩展Illuminate \ Console \ Command的控制台命令时,“ composer dump-autoload”失败并显示错误消息:
c:\Program Files (x86)\Ampps\www\ptest>composer dump-autoload
Generating optimized autoload files> Illuminate\Foundation\ComposerScripts::postAutoloadDump
> @php artisan package:discover --ansi
ReflectionException : Class TestVendor\TestPackage\TestCommand does not exist
at C:\Program Files (x86)\Ampps\www\ptest\vendor\laravel\framework\src\Illuminate\Container\Container.php:790
786| if ($concrete instanceof Closure) {
787| return $concrete($this, $this->getLastParameterOverride());
788| }
789|
> 790| $reflector = new ReflectionClass($concrete);
791|
792| // If the type is not instantiable, the developer is attempting to resolve
793| // an abstract type such as an Interface or Abstract Class and there is
794| // no binding registered for the abstractions so we need to bail out.
Exception trace:
1 ReflectionClass::__construct("TestVendor\TestPackage\TestCommand")
C:\Program Files (x86)\Ampps\www\ptest\vendor\laravel\framework\src\Illuminate\Container\Container.php:790
2 Illuminate\Container\Container::build("TestVendor\TestPackage\TestCommand")
C:\Program Files (x86)\Ampps\www\ptest\vendor\laravel\framework\src\Illuminate\Container\Container.php:667
我尝试在C:\ Program Files(x86)\ Ampps \ www \ ptest \ packages文件夹中手动创建软件包,并且尝试使用打包程序https://github.com/Jeroen-G/laravel-packager,但两者的结果相同案例。
TestCommand.php
<?php
namespace TestVendor\TestPackage;
use Illuminate\Console\Command;
class TestCommand extends Command {
protected $signature = 'test:hello';
protected $description = 'say hello';
public function __construct()
{
parent::__construct();
}
public function handle()
{
$this->info("hello!");
}
}
TestServiceProvider.php
<?php
namespace TestVendor\TestPackage;
use Illuminate\Support\ServiceProvider;
class TestServiceProvider extends ServiceProvider
{
public function boot()
{
if ($this->app->runningInConsole()) {
$this->bootForConsole();
}
}
public function register()
{
$this->mergeConfigFrom(__DIR__.'/../config/testpackage.php', 'testpackage');
$this->app->singleton('testpackage', function ($app) {
return new TestPackage;
});
}
public function provides()
{
return ['testpackage'];
}
protected function bootForConsole()
{
// Registering package commands.
$this->commands([TestCommand::class]);
}
}
当我直接从命令行执行TestCommand.php文件时,它失败并显示错误消息
PHP Fatal error: Class 'Illuminate\Console\Command' not found
我已经检查了“ Vendor”文件夹中的其他工作包,并且它们的结构都与我的包相同。自动加载似乎无法正常工作。
答案 0 :(得分:0)
“控制台”文件夹位于“ src”文件夹之外。因此无法发现它。
答案 1 :(得分:0)
删除供应商和node_modules文件夹 然后运行以下命令
composer update
npm install
应该工作正常。