Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 4: Deploy custom artisan command in package

I have developed some custom artisan command for easier use with my package. Is it possible to include the artisan command into the package for easier deployment? If can, how?

Thanks.

like image 559
user1995781 Avatar asked Mar 17 '14 14:03

user1995781


1 Answers

Having a command set in your package structure:

<?php namespace App\Artisan;

use Illuminate\Console\Command;

class MyCommand extends Command {

    protected $name = 'mypackage:mycommand';

    protected $description = 'Nice description of my command.';

    public function fire()
    {
        /// do stuff
    }

}

You can, in your package Service Provider:

<?php namespace App;

use Illuminate\Support\ServiceProvider;
use App\Artisan\MyCommand;

class MyServiceProvider extends ServiceProvider {

    public function register()
    {
        $this->registerMyCommand();

        $this->commands('mycommand');
    }

    private function registerMyCommand()
    {
        $this->app['mycommand'] = $this->app->share(function($app)
        {
            return new MyCommand;
        });
    }

}

The trick is in the line

$this->commands('mycommand');

Which tells Laravel to add your command to the artisan list of commands available.

like image 161
Antonio Carlos Ribeiro Avatar answered Nov 09 '22 04:11

Antonio Carlos Ribeiro