0
我正在爲我的程序包創建命令。程序包命令中的依賴注入
我的構造函數是:
public function __construct(\Artisan $artisan)
{
parent::__construct();
$this->artisan = $artisan;
}
保護$artisan
財產,當然是存在的。
在我的服務提供商register()
方法我已經嘗試了幾種註冊命令的方法。
第一:
$this->app['custom.command'] = $this->app->share(function ($app)
{
return new CustomCommand(new \Artisan);
});
$this->commands('custom.command');
二:
$this->app['custom.command'] = $this->app->share(function ($app)
{
return $app->make('CustomCommand');
});
$this->commands('custom.command');
通常情況下,它應該工作。但是當我運行這個命令時,只要我在我的fire()
方法中運行$this->artisan->call('migrate')
,我總是會得到Call to undefined method Illuminate\Support\Facades\Artisan::call()
錯誤消息。
但是,當我寫\Artisan::call('migrate')
而不是$this->artisan->call('migrate')
一切工作正常。
有人知道我做錯了什麼嗎?
在此先感謝。
OMG,我想到了好幾次,但始終拒絕它作爲壞主意。非常感謝你!有用。 – Alexxali