The Console Component

The Console component eases the creation of beautiful and testable commandline interfaces.

The Console component allows you to create command-line commands. Your consolecommands can be used for any recurring task, such as cronjobs, imports, orother batch jobs.

Installation

  1. $ composer require symfony/console

Note

If you install this component outside of a Symfony application, you mustrequire the vendor/autoload.php file in your code to enable the classautoloading mechanism provided by Composer. Readthis article for more details.

Creating a Console Application

This article explains how to use the Console features as an independentcomponent in any PHP application. Read the Console Commands article tolearn about how to use it in Symfony applications.

First, you need to create a PHP script to define the console application:

  1. #!/usr/bin/env php
  2. <?php
  3. // application.php
  4.  
  5. require __DIR__.'/vendor/autoload.php';
  6.  
  7. use Symfony\Component\Console\Application;
  8.  
  9. $application = new Application();
  10.  
  11. // ... register commands
  12.  
  13. $application->run();

Then, you can register the commands usingadd():

  1. // ...
  2. $application->add(new GenerateAdminCommand());

See the Console Commands article for information about how to create commands.

Learn more