Skip to content
Advertisement

How to register console command from package in Laravel 5?

Since Laravel 5, it is interest to me – how to register and use console command from package in Laravel 5.

As in laracast discuss https://laracasts.com/discuss/channels/tips/developing-your-packages-in-laravel-5, i create a directory structure, add my package to autoload and create a service provider

<?php namespace GoodvinEternalTree;

use ConsoleInstallCommand;
use IlluminateSupportServiceProvider;

class EternalTreeServiceProvider extends ServiceProvider
{
    public function boot()
    {

    }

    public function register()
    {
        $this->registerCommands();
    }

    protected function registerCommands()
    {
        $this->registerInstallCommand();
    }

    protected function registerInstallCommand()
    {
        $this->app->singleton('command.eternaltree.install', function($app) {

            return new InstallCommand();
        });
    }

    public function provides()
    {
        return [

            'command.eternaltree.install'
        ];
    }
}

My InstallCommand.php script stored in /src/Console

<?php namespace GoodvinEternalTreeConsole;

use IlluminateConsoleCommand;
use SymfonyComponentConsoleInputInputOption;
use SymfonyComponentConsoleInputInputArgument;

class InstallCommand extends Command {

    protected $name = 'install:eternaltree';
    protected $description = 'Command for EternalTree migration & model install.';

    public function __construct()
    {
        parent::__construct();
    }

    public function fire()
    {
        $this->info('Command eternaltree:install fire');
    }

}

I register service provider in app.php & execute dump-autoload. But when i try to execute

php artisan eternaltree:install

it show me

[InvalidArgumentException] There are no commands defined in the "eternaltree" namespace.

I think my command is not registered by service provider, because php artisan list does not show my command. Can anyone explain to me what is the right way to register commands in own package in Laravel 5 ?

Advertisement

Answer

I found a decision, it was simple:

in registerInstallCommand() Add

$this->commands('command.eternaltree.install');
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement