Support the ongoing development of Laravel.io →
Configuration Packages

Hi!

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 Goodvin\EternalTree;

use Console\InstallCommand;
use Illuminate\Support\ServiceProvider;

class EternalTreeServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap the application events.
     *
     * @return void
     */
    public function boot()
    {
        // Publish a config file
        $this->publishes([

            __DIR__.'/config/eternaltree.php' => config_path('eternaltree.php'),
        ]);
    }

    /**
     * Register the service provider.
     *
     * @return void
     */
    public function register()
    {
        $this->registerCommands();
    }

    /**
     * Register the commands.
     *
     * @return void
     */
    protected function registerCommands()
    {
        $this->registerInstallCommand();
    }

    /**
     * Register the 'eternaltree:install' command.
     *
     * @return void
     */
    protected function registerInstallCommand()
    {
        $this->app->singleton('command.eternaltree.install', function($app) {

            return new InstallCommand();
        });
    }

    /**
     * Get the services provided by the provider.
     *
     * @return array
     */
    public function provides()
    {
        return [

            'command.eternaltree.install'
        ];
    }
}

My InstallCommand.php script stored in /src/Console

<?php namespace Goodvin\EternalTree\Console;

use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;

class InstallCommand extends Command {

	/**
	 * The console command name.
	 *
	 * @var string
	 */
	protected $name = 'eternaltree:install';

	/**
	 * The console command description.
	 *
	 * @var string
	 */
	protected $description = 'Command for EternalTree migration & model install.';

	/**
	 * Create a new command instance.
	 *
	 * @return void
	 */
	public function __construct()
	{
		parent::__construct();
	}

	/**
	 * Execute the console command.
	 *
	 * @return mixed
	 */
	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 ?

Best regards, Evgeniy.

Last updated 3 years ago.
0

I found a decision, it was simple:

in registerInstallCommand() add $this->commands('command.eternaltree.install');

0

Sign in to participate in this thread!

Eventy

Your banner here too?

esomkin esomkin Joined 18 Mar 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.