Laravel 4: Deploy custom artisan command in package

自闭症网瘾萝莉.ら 提交于 2019-12-20 14:39:17

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/22456771/laravel-4-deploy-custom-artisan-command-in-package

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!