Метод createTable
Метод createTable класса SymfonyStyle создаёт и возвращает объект Table, который позволяет выводить данные в виде таблицы в консольном приложении Symfony. Метод не принимает обязательных параметров и возвращает готовый к настройке объект таблицы.
Синтаксис
$table = $io->createTable();
Пример
Давайте создадим простую таблицу с двумя колонками и одной строкой данных:
<?php
namespace AppCommand;
use SymfonyComponentConsoleCommandCommand;
use SymfonyComponentConsoleInputInputInterface;
use SymfonyComponentConsoleOutputOutputInterface;
use SymfonyComponentConsoleStyleSymfonyStyle;
class TableCommand extends Command
{
protected static $defaultName = 'app:table';
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$table = $io->createTable();
$table->setHeaders(['id', 'name']);
$table->addRow([1, 'abcde']);
$table->render();
return Command::SUCCESS;
}
}
?>
Результат выполнения кода:
+----+-------+
| id | name |
+----+-------+
| 1 | abcde |
+----+-------+
Пример
Давайте создадим таблицу с несколькими строками и настроим стиль отображения:
<?php
namespace AppCommand;
use SymfonyComponentConsoleCommandCommand;
use SymfonyComponentConsoleHelperTableTable;
use SymfonyComponentConsoleInputInputInterface;
use SymfonyComponentConsoleOutputOutputInterface;
use SymfonyComponentConsoleStyleSymfonyStyle;
class TableCommand extends Command
{
protected static $defaultName = 'app:table';
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$table = $io->createTable();
$table->setHeaders(['id', 'name', 'active']);
$table->setRows([
[1, 'abcde', 'yes'],
[2, 'article', 'no'],
[3, 'user', 'yes'],
]);
$table->render();
return Command::SUCCESS;
}
}
?>
Результат выполнения кода:
+----+---------+--------+
| id | name | active |
+----+---------+--------+
| 1 | abcde | yes |
| 2 | article | no |
| 3 | user | yes |
+----+---------+--------+
Смотрите также
-
класс
SymfonyStyle,
который предоставляет методы для красивого вывода в консоль -
метод
table,
который выводит готовую таблицу одной строкой -
метод
listing,
который выводит нумерованный список элементов -
метод
title,
который выводит заголовок команды