【问题标题】:Illuminate\Console\Command not found when developing laravel 5.8 package开发 laravel 5.8 包时找不到 Illuminate\Console\Command
【发布时间】:2019-12-25 10:14:08
【问题描述】:

我正在为 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

我已经检查了“供应商”文件夹中的其他工作包,它们都与我的包具有相同的结构。似乎自动加载无法正常工作。

【问题讨论】:

  • 您是否对您的错误进行了一些研究?
  • 在构建命令时,不应直接调用文件。你应该通过artisan 命令调用它们(这将启动 Laravel),就像它们在the manual 中显示的一样。另外,您正在使用命名空间TestVendor,是否已将其添加到composer.json 文件中以便PHP 知道在哪里可以找到它?
  • 我知道命令应该使用 artisan 来调用。直接调用命令是一种快速判断文件是否存在语法错误的方法。我花了大约 5 个小时试图解决这个问题。 TestVendor 命名空间位于 composer.json 文件中 - 这是 github.com/Jeroen-G/laravel-packager 自动执行的操作
  • 我通常会让我的 IDE 告诉我是否有任何语法错误。无论如何,您是否在您的composer.json 中注册了命名空间TestVendor
  • 感谢您的提示。命名空间已经在 autoload/psr-4 部分。但是“控制台”文件夹位于“src”文件夹之外。因此无法被发现。

标签: php laravel


【解决方案1】:

删除 vendor 和 node_modules 文件夹 然后运行以下命令

composer update
npm install

它应该可以正常工作。

【讨论】:

    【解决方案2】:

    “控制台”文件夹位于“src”文件夹之外。因此无法被发现。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-07-21
      • 2019-09-16
      • 2018-12-26
      • 2020-03-25
      • 1970-01-01
      • 2015-12-10
      • 1970-01-01
      • 2017-07-07
      相关资源
      最近更新 更多