Hello everyone!

We have been investing plenty of personal time and energy for many years to share our knowledge with you all. However, we now need your help to keep this blog running. All you have to do is just click one of the adverts on the site, otherwise it will sadly be taken down due to hosting etc. costs. Thank you.

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 |
+--------+---------+--------+