Reputation: 1193
Explain me pls. How to execute commandline commands, from my controller.
for example i want to start icecast server
of course i can use exec('icecast2 run -c path/to/config.xml')
is there Laravel way to do this?
Upvotes: 0
Views: 1575
Reputation: 4026
Create macro
in your Envoy.blade.php
, read more: https://laravel.com/docs/5.2/envoy
@macro('deploy')
//your commands here
@endmacro
The yo can call it through the Symfony Process (http://symfony.com/doc/current/components/process.html#usage), like so:
$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
});
It even better to create some external class for this.
<?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;
}
}
And call it from controller:
public function store(Request $request, Envoy $envoy)
{
$group = $this->group->create($request->all());
$result = $envoy->run('<some command>');
// Do something with $result...
}
Credits: https://laracasts.com/discuss/channels/general-discussion/run-envoy-from-controller
Upvotes: 2