Quantcast
Channel: WilliaBlog.Net
Viewing all articles
Browse latest Browse all 18

How to save/redirect output from Laravel Artisan command

$
0
0

Sometimes you need to call one Artisan Command from another, and the framework has a "call" method to handle that eventuality. Sometimes, you even need to return data from one command and process it with another, and that has always been a little tricky. The command you are calling to must write the data to the console using:

$this->line($data);

In Laravel 4 you could do this to call that command and retrieve the data:

use Symfony\Component\Console\Output\BufferedOutput;

$output = new BufferedOutput;    
$this->call('command:name', array('argument' => 'foo', '--option' => 'bar'), $output);
$result = $output->fetch();

And that worked great. However, I recently upgraded one of my PHP sites from Laravel 4 to Laravel 5 and sadly this no longer works. The reason is that the optional output argument is no longer an option. However, after looking at the code in the Command class I was able to find a way to make this work:

use Symfony\Component\Console\Output\BufferedOutput;

$output = new BufferedOutput;
$instance = $this->getApplication()->find('command:name');
$arguments = array('command' => 'command:name', 'argument' => 'foo', '--option' => 'bar');
$instance->run(new ArrayInput($arguments), $output);
$result = $output->fetch();

If the data being returned is more complex than a simple value, convert it to Json: Write it out using $this->line(json_encode(array($thumbnailList, $posterList))); and read it like this: $images = json_decode($output->fetch(), true);

 


Viewing all articles
Browse latest Browse all 18

Latest Images

Trending Articles





Latest Images