CLI Frameworks for PHP: Creating Console Applications with Symfony Console and Laravel Zero

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

CLI Frameworks for PHP: From Simple Scripts to Professional Console Applications



Console applications (CLI — Command Line Interface) remain an integral part of modern development. They are used for task automation, data processing, migrations, running tests, and server administration. PHP, despite its reputation as a "web language," provides excellent capabilities for creating powerful CLI tools. In this article, we will look at the best frameworks and libraries for developing console applications in PHP that will help you move from simple scripts to structured, testable, and scalable solutions.



Why Use a CLI Framework Instead of a Simple Script?

Writing a console script "from scratch" seems simple, but quickly leads to problems:

  • Argument handling: manually parsing $argv and $argc is tedious and error-prone.
  • Output formatting: colors, progress bars, tables — all require separate implementation.
  • Code structure: it's difficult to organize multiple commands in one file.
  • Testing: console scripts are hard to test without special tools.

Using a framework solves these problems and provides a ready-made architecture, DI container, event system, and integration with popular libraries.



Symfony Console — The Gold Standard

Symfony Console is a component of the Symfony ecosystem that has become the de facto standard for creating CLI applications in PHP. It is used in Laravel (via Artisan), Doctrine, Composer, and many other projects.



Installation and Basic Example

composer require symfony/console


Let's create a simple command that greets the user:

<?php// greet.phprequire __DIR__ . '/vendor/autoload.php';

use Symfony\\Component\\Console\\Application;use Symfony\\Component\\Console\\Command\\Command;use Symfony\\Component\\Console\\Input\\InputInterface;use Symfony\\Component\\Console\\Output\\OutputInterface;use Symfony\\Component\\Console\\Input\\InputArgument;

class GreetCommand extends Command{ protected static $defaultName = 'app:greet';

protected function configure(): void { $this->setDescription('Greets the user') ->addArgument('name', InputArgument::REQUIRED, 'User name'); }

protected function execute(InputInterface $input, OutputInterface $output): int { $name = $input->getArgument('name'); $output->writeln("Hello, <info>$name</info>!"); return Command::SUCCESS; }}

$application = new Application();$application->add(new GreetCommand());$application->run();


Run: php greet.php app:greet Ivan will output Hello, Ivan! in green color.



Key Features of Symfony Console

  • Automatic help generation: the list and help commands work "out of the box".
  • Argument and option types: required/optional, arrays, default values.
  • Output formatting: tags like <info>, <comment>, <error> for colors.
  • Progress bars and tables: built-in ProgressBar and Table classes.
  • Question support: interactive input via QuestionHelper.
  • Events and lifecycle: you can intercept command execution.


Example with a Progress Bar

use Symfony\\Component\\Console\\Helper\\ProgressBar;

protected function execute(InputInterface $input, Outpu

Recommendations