2016-09-08 7 views
0

私に説明してください。私のコントローラからコマンドラインコマンドを実行する方法。Laravel - 現在のサーバーでconsoleコマンドを実行します。

例えば、私はexec('icecast2 run -c path/to/config.xml')

を使用することができ、もちろんのIcecastのサーバー

を開始するには、これを行うにはLaravelの方法は何ですか?

+0

https://laravel.com/docs/5.2/envoy – GONG

+0

@GONGうん、すでにこれを見たが、私私のコントローラからenvoyコマンドを呼び出す方法を取得できません – ArtemSky

+1

私はこれを行うために "ララベルの方法"があるとは思わない。あなたは 'exec'か' shell_exec'を使うことができます。 – Jonathon

答えて

1

続きを読む、あなたのEnvoy.blade.phpmacroを作成します。https://laravel.com/docs/5.2/envoy

@macro('deploy') 
    //your commands here 
@endmacro 

ヨーヨーはそうのように、symfonyのプロセス(http://symfony.com/doc/current/components/process.html#usage)を介して、それを呼び出すことができます。

$process = new Process("/home/$user/.composer/vendor/bin/envoy run deploy"); 
$process->setTimeout(3600); 
$process->setIdleTimeout(300); 
$process->setWorkingDirectory(base_path()); 
$process->run(function ($type, $buffer) 
{ 
    //print output 
}); 

それはさらに良いいくつかを作成しますこのための外部クラス。

<?php 

namespace App\Services; 

use Symfony\Component\Process\Process; 

class Envoy 
{ 

public function run($task, $live = false) 
{ 
    $result = []; 

    $process = new Process('~/.composer/vendor/bin/envoy run '. $task); 
    $process->setTimeout(3600); 
    $process->setIdleTimeout(300); 
    $process->setWorkingDirectory(base_path()); 
    $process->run(
     function ($type, $buffer) use ($live, &$result) { 
      $buffer = str_replace('[127.0.0.1]: ', '', $buffer); 

      if ($live) { 
       echo $buffer . '</br />'; 
      } 

      $result[] = $buffer; 
     } 
    ); 

    return $result; 
} 
} 

とコントローラからそれを呼び出す:

public function store(Request $request, Envoy $envoy) 
{ 
    $group = $this->group->create($request->all()); 

    $result = $envoy->run('<some command>'); 

    // Do something with $result... 
} 

クレジット:https://laracasts.com/discuss/channels/general-discussion/run-envoy-from-controller

関連する問題