【问题标题】:How to save/redirect output from Laravel 5 Artisan command?如何保存/重定向 Laravel 5 Artisan 命令的输出?
【发布时间】:2015-04-24 02:45:51
【问题描述】:

我尝试了described here 的方法,但这不适用于我的 Laravel 5 安装。

use Symfony\Component\Console\Output\BufferedOutput;

Route::get('/test', function()
{
    $output = new BufferedOutput;

    Artisan::call('testCommand', array(), $output);

    return $output->fetch();
});

我的命令;

public function fire()
{
    $this->info('No output visible');
}

有什么建议我可能做错了吗?还是在 Laravel 5 中发生了一些变化?

【问题讨论】:

    标签: php laravel laravel-5 laravel-artisan


    【解决方案1】:

    我也遇到了同样的问题,用 oldschool PHP 替换 BufferedOutput 让它对我有用,也许它也对你有用:

    Route::get('/test', function()
    {
        ob_start();
        Artisan::call('testCommand');
        $output = ob_get_clean();
        return $output;
    });
    

    【讨论】:

    • 对我不起作用。我正在使用命令 shell 和 PHPUnit,并且 $output 始终为空。
    【解决方案2】:

    如果您在命令行之外工作,则可以通过管道通过tee 命令同时写入文件和stdout

    php artisan <command> | tee <filename>
    

    【讨论】:

      【解决方案3】:

      我设法使用Artisan::output() 让它工作,它返回最新命令的输出。

      Route::get('/test', function()
      {    
          Artisan::call('testCommand', array());
      
          return Artisan::output();
      });
      

      应该为你做。

      【讨论】:

        【解决方案4】:

        我做了这个

        php artisan your:command >> output.txt
        

        对我来说效果很好。

        【讨论】:

          猜你喜欢
          • 2013-12-05
          • 2023-04-02
          • 2015-04-14
          • 2016-09-11
          • 2014-10-12
          • 1970-01-01
          • 1970-01-01
          • 2018-03-17
          • 2014-04-01
          相关资源
          最近更新 更多