Laravel 4: 如何在包中部署自定义Artisan命令

5
我为了更方便地使用我的包,开发了一些自定义的Artisan命令。是否可以将这些Artisan命令包含在该包中,以便更轻松地部署?如果可以,应该如何操作?
谢谢。
1个回答

13
在您的软件包结构中具备命令集:
<?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
    }

}

在你的包服务提供商中,你可以:

<?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;
        });
    }

}

关键在于那条线

$this->commands('mycommand');

这会告诉 Laravel 把你的命令添加到可用的 artisan 命令列表中。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接