Example below produces a colourful table as an output.


Example


namespace Application\BackendBundle\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class HelloCommand extends ContainerAwareCommand
{
private $people = [
['name' => 'Robert', 'surname' => 'DeNiro', 'colour' => 'red'],
['name' => 'Al', 'surname' => 'Pacino', 'colour' => 'green'],
['name' => 'Andy', 'surname' => 'Garcia', 'colour' => 'yellow']
];

protected function configure()
{
$this
->setName('say:hello')
->setDescription('This says hello to some people');
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$headers = [
"NAME",
"SURNAME",
"COLOUR"
];

$rows = [];
foreach ($this->people as $people) {
$name = $people['name'];
$surname = $people['surname'];
$colour = $people['colour'];
$rows[] = [
"$name",
"$surname",
"$colour"
];
}

$table = new Table($output);
$table
->setHeaders($headers)
->setRows($rows)
->setStyle('default');
$table->render();
}
}

Run


php app/console say:hello
+--------+---------+--------+
| NAME | SURNAME | COLOUR |
+--------+---------+--------+
| Robert | DeNiro | red |
| Al | Pacino | green |
| Andy | Garcia | yellow |
+--------+---------+--------+