How to save/redirect output from Laravel Artisan command?

[亡魂溺海] 提交于 2019-12-28 05:54:07

问题


I'm using Artisan::call() in one of my routes and would like to save the command output to a variable.

Is there any way to capture the STDOUT and STDERR generated by the artisan command?


回答1:


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



回答2:


Seems the previous answers don't work in Laravel 5.2 any more (not sure about 5.1) You can now use 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);



回答3:


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


来源:https://stackoverflow.com/questions/20111287/how-to-save-redirect-output-from-laravel-artisan-command

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