Skip to content
Advertisement

IlluminateConsoleCommand 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 IlluminateConsoleCommand then “composer dump-autoload” fails with error message:

c:Program Files (x86)Amppswwwptest>composer dump-autoload
    Generating optimized autoload files> IlluminateFoundationComposerScripts::postAutoloadDump
    > @php artisan package:discover --ansi

       ReflectionException  : Class TestVendorTestPackageTestCommand does not exist

      at C:Program Files (x86)AmppswwwptestvendorlaravelframeworksrcIlluminateContainerContainer.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("TestVendorTestPackageTestCommand")
          C:Program Files (x86)AmppswwwptestvendorlaravelframeworksrcIlluminateContainerContainer.php:790

      2   IlluminateContainerContainer::build("TestVendorTestPackageTestCommand")
          C:Program Files (x86)AmppswwwptestvendorlaravelframeworksrcIlluminateContainerContainer.php:667

I have tried to create the package by hand inside of C:Program Files (x86)Amppswwwptestpackages 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 TestVendorTestPackage;

    use IlluminateConsoleCommand;

    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 TestVendorTestPackage;

    use IlluminateSupportServiceProvider;

    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 'IlluminateConsoleCommand' 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.

Advertisement

Answer

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

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement