简体   繁体   中英

Illuminate\Console\Command not found when developing laravel 5.8 package

I am developing a package for Laravel 5.8. When I try to create a console command that extends Illuminate\\Console\\Command then "composer dump-autoload" fails with error message:

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

I have tried to create the package by hand inside of C:\\Program Files (x86)\\Ampps\\www\\ptest\\packages folder and I tried to use the packager https://github.com/Jeroen-G/laravel-packager but the result is identical in both cases.

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]);
        }
    }

When I execute the TestCommand.php file directly from command line it fails with the error message

PHP Fatal error:  Class 'Illuminate\Console\Command' not found

I have checked other working packages inside "Vendor" folder and all have the same structure as my package. It seems as if autoloading does not work properly.

The "console" folder was outside of "src" folder. Therefore it could not be discovered.

remove vendor and node_modules folder then run following commands

composer update
npm install

it should work fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM