我为了更方便地使用我的包,开发了一些自定义的Artisan命令。是否可以将这些Artisan命令包含在该包中,以便更轻松地部署?如果可以,应该如何操作?
谢谢。
谢谢。
<?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 命令列表中。