The Tree Helper allows you to build and display tree structures in the console. It's commonly used to render directory hierarchies, but you can also use it to render any tree-like content, such us organizational charts, product category trees, taxonomies, etc.
.. versionadded:: 7.3 The ``TreeHelper`` class was introduced in Symfony 7.3.
The :method:`Symfony\\Component\\Console\\Helper\\TreeHelper::createTree` method creates a tree structure from an array and returns a :class:`Symfony\\Component\\Console\\Helper\\Tree` object that can be rendered in the console.
You can build a tree from an array by passing the array to the :method:`Symfony\\Component\\Console\\Helper\\TreeHelper::createTree` method inside your console command:
namespace App\Command; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Helper\TreeHelper; use Symfony\Component\Console\Helper\TreeNode; use Symfony\Component\Console\Style\SymfonyStyle; #[AsCommand(name: 'app:my-command', description: '...')] class MyCommand { // ... public function __invoke(SymfonyStyle $io): int { $node = TreeNode::fromValues([ 'config/', 'public/', 'src/', 'templates/', 'tests/', ]); $tree = TreeHelper::createTree($io, $node); $tree->render(); // ... } }
This exampe would output the following:
├── config/
├── public/
├── src/
├── templates/
└── tests/
The given contents can be defined in a multi-dimensional array:
$tree = TreeHelper::createTree($io, null, [ 'src' => [ 'Command', 'Controller' => [ 'DefaultController.php', ], 'Kernel.php', ], 'templates' => [ 'base.html.twig', ], ]); $tree->render();
The above code will output the following tree:
├── src
│ ├── Command
│ ├── Controller
│ │ └── DefaultController.php
│ └── Kernel.php
└── templates
└── base.html.twig
You can build a tree by creating a new instance of the :class:`Symfony\\Component\\Console\\Helper\\Tree` class and adding nodes to it:
use Symfony\Component\Console\Helper\TreeHelper; use Symfony\Component\Console\Helper\TreeNode; $node = TreeNode::fromValues([ 'Command', 'Controller' => [ 'DefaultController.php', ], 'Kernel.php', ]); $node->addChild('templates'); $node->addChild('tests'); $tree = TreeHelper::createTree($io, $node); $tree->render();
The tree helper provides a few built-in styles that you can use to customize the output of the tree:
use Symfony\Component\Console\Helper\TreeStyle; // ... $tree = TreeHelper::createTree($io, $node, [], TreeStyle::compact()); $tree->render();
TreeHelper::createTree($io, $node, [], TreeStyle::default())
(details)
├── config
│ ├── packages
│ └── routes
│ ├── framework.yaml
│ └── web_profiler.yaml
├── src
│ ├── Command
│ ├── Controller
│ │ └── DefaultController.php
│ └── Kernel.php
└── templates
└── base.html.twig
TreeHelper::createTree($io, $node, [], TreeStyle::box())
(details)
┃╸ config
┃ ┃╸ packages
┃ ┗╸ routes
┃ ┃╸ framework.yaml
┃ ┗╸ web_profiler.yaml
┃╸ src
┃ ┃╸ Command
┃ ┃╸ Controller
┃ ┃ ┗╸ DefaultController.php
┃ ┗╸ Kernel.php
┗╸ templates
┗╸ base.html.twig
TreeHelper::createTree($io, $node, [], TreeStyle::doubleBox())
(details)
╠═ config
║ ╠═ packages
║ ╚═ routes
║ ╠═ framework.yaml
║ ╚═ web_profiler.yaml
╠═ src
║ ╠═ Command
║ ╠═ Controller
║ ║ ╚═ DefaultController.php
║ ╚═ Kernel.php
╚═ templates
╚═ base.html.twig
TreeHelper::createTree($io, $node, [], TreeStyle::compact())
(details)
├ config
│ ├ packages
│ └ routes
│ ├ framework.yaml
│ └ web_profiler.yaml
├ src
│ ├ Command
│ ├ Controller
│ │ └ DefaultController.php
│ └ Kernel.php
└ templates
└ base.html.twig
TreeHelper::createTree($io, $node, [], TreeStyle::light())
(details)
|-- config
| |-- packages
| `-- routes
| |-- framework.yaml
| `-- web_profiler.yaml
|-- src
| |-- Command
| |-- Controller
| | `-- DefaultController.php
| `-- Kernel.php
`-- templates
`-- base.html.twig
TreeHelper::createTree($io, $node, [], TreeStyle::minimal())
(details)
. config
. . packages
. . routes
. . framework.yaml
. . web_profiler.yaml
. src
. . Command
. . Controller
. . . DefaultController.php
. . Kernel.php
. templates
. base.html.twig
TreeHelper::createTree($io, $node, [], TreeStyle::rounded())
(details)
├─ config
│ ├─ packages
│ ╰─ routes
│ ├─ framework.yaml
│ ╰─ web_profiler.yaml
├─ src
│ ├─ Command
│ ├─ Controller
│ │ ╰─ DefaultController.php
│ ╰─ Kernel.php
╰─ templates
╰─ base.html.twig
You can create your own tree style by passing the characters to the constructor of the :class:`Symfony\\Component\\Console\\Helper\\TreeStyle` class:
use Symfony\Component\Console\Helper\TreeHelper; use Symfony\Component\Console\Helper\TreeStyle; $customStyle = new TreeStyle('🟣 ', '🟠 ', '🔵 ', '🟢 ', '🔴 ', '🟡 '); // Pass the custom style to the createTree method $tree = TreeHelper::createTree($io, null, [ 'src' => [ 'Command', 'Controller' => [ 'DefaultController.php', ], 'Kernel.php', ], 'templates' => [ 'base.html.twig', ], ], $customStyle); $tree->render();
The above code will output the following tree:
🔵 🟣 🟡 src
🔵 🟢 🟣 🟡 Command
🔵 🟢 🟣 🟡 Controller
🔵 🟢 🟢 🟠 🟡 DefaultController.php
🔵 🟢 🟠 🟡 Kernel.php
🔵 🟠 🟡 templates
🔵 🔴 🟠 🟡 base.html.twig