如何保存/重定向 Laravel Artisan 命令的输出?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20111287/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to save/redirect output from Laravel Artisan command?
提问by Dave
I'm using Artisan::call()
in one of my routes and would like to save the command output to a variable.
我Artisan::call()
在我的一条路线中使用,并希望将命令输出保存到一个变量中。
Is there any way to capture the STDOUT and STDERR generated by the artisan command?
有什么办法可以捕获artisan命令生成的STDOUT和STDERR吗?
回答by Antonio Carlos Ribeiro
This is a way:
这是一种方式:
use Symfony\Component\Console\Output\BufferedOutput;
Route::get('/test', function()
{
$output = new BufferedOutput;
Artisan::call('list', array(), $output);
return $output->fetch();
});
回答by Arno van Oordt
Seems the previous answers don't work in Laravel 5.2 any more (not sure about 5.1)
You can now use Artisan::output();
似乎以前的答案不再适用于 Laravel 5.2(不确定 5.1)您现在可以使用 Artisan::output();
$output = '';
if (!Schema::hasTable('migrations')) {
Artisan::call('migrate:install', array());
$output .= Artisan::output();
}
// Updates the migration, then seed the database
Artisan::call('migrate:refresh', array('--force' => 1));
$output .= Artisan::output();
Artisan::call('db:seed', array('--force' => 1));
$output .= Artisan::output();
dd($output);
回答by Vincent Mimoun-Prat
When running a command from inside another command, here is how to get all the styling:
从另一个命令内部运行命令时,以下是获取所有样式的方法:
public function handle()
{
Artisan::call('other:command', [], $this->getOutput());
}