【问题标题】:How to save/redirect output from Laravel Artisan command?如何保存/重定向 Laravel Artisan 命令的输出?
【发布时间】:2013-12-05 08:26:12
【问题描述】:

我在我的一条路由中使用Artisan::call(),并希望将命令输出保存到一个变量中。

有没有什么办法可以捕捉到artisan命令生成的STDOUT和STDERR?

【问题讨论】:

    标签: php laravel laravel-4


    【解决方案1】:

    从另一个命令内部运行命令时,以下是获取所有样式的方法:

    public function handle()
    {
        Artisan::call('other:command', [], $this->getOutput());
    }
    

    【讨论】:

      【解决方案2】:

      似乎以前的答案在 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);
      

      【讨论】:

        【解决方案3】:

        这是一种方式:

        use Symfony\Component\Console\Output\BufferedOutput;
        
        Route::get('/test', function()
        {
            $output = new BufferedOutput;
        
            Artisan::call('list', array(), $output);
        
            return $output->fetch();
        });
        

        【讨论】:

        猜你喜欢
        • 2015-04-24
        • 2014-10-12
        • 1970-01-01
        • 1970-01-01
        • 2018-03-17
        • 2014-04-01
        • 2011-01-25
        • 2020-06-13
        相关资源
        最近更新 更多