March 29, 2018 Javier Eguiluz

The Console component is the second most popular Symfony component (more than 82 million downloads!) and it's packed with amazing features. In Symfony 4.1 we improved it even more with a feature to create and manipulate multiple output sections.

Currently, displaying information in a command console is a pretty basic operation:

1 2 3 4 5 6 7 8 9 10 11 class MyCommand extends Command { protected function execute ( InputInterface $input , OutputInterface $output ) { $output -> writeln ( 'Display some information...' ); // ... $output -> writeln ( 'Display more information...' ); } }

In Symfony 4.1 you'll be able to display information, overwrite it, delete part of it and update different parts of the output simultaneously. The new operations are based on "output sections" which are independently managed regions of the console output:

1 2 3 4 5 6 7 8 9 class MyCommand extends Command { protected function execute ( InputInterface $input , OutputInterface $output ) { $section = $output -> section (); $section -> writeln ( 'Display some information...' ); // ... } }

Overwriting output contents¶ The new overwrite() method deletes all section contents and replaces them with the given contents: 1 2 3 4 5 6 7 $section = $output -> section (); $section -> writeln ( 'Downloading the file...' ); // ... $section -> overwrite ( 'Uncompressing the file...' ); // ... $section -> overwrite ( 'Copying the contents...' ); // ...

Deleting output contents¶ The new clear(int $numLines) method deletes the last $numLines of the section (or all contents if no argument is provided): 1 2 3 4 5 6 7 8 $section = $output -> section (); $section -> writeln ( 'Welcome to the installation Process!' ); $section -> writeln ( 'Downloading the file...' ); $section -> writeln ( 'Uncompressing the file...' ); $section -> writeln ( 'Copying the contents...' ); // ... $section -> clear ( 3 ); $section -> writeln ( 'The installation is complete!' );

Appending rows to rendered tables¶ In previous Symfony versions, displaying a table required to know all its rows and columns. However, the new appendRow() method, which works as the existing addRow() method, allows to add new rows for already displayed tables: 1 2 3 4 5 6 7 8 9 $section = $output -> section (); $table = new Table ( $section ); $table -> addRow ([ 'Row 1' ]); // display the table with the known contents $table -> render (); // add a new row with new contents to the already displayed table $table -> appendRow ([ 'Row 2' ]);