The Console Component

The Console Component

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

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

Installation

  1. $ composer require symfony/console

Note

If you install this component outside of a Symfony application, you must require the vendor/autoload.php file in your code to enable the class autoloading mechanism provided by Composer. Read this article for more details.

Creating a Console Application

See also

This article explains how to use the Console features as an independent component in any PHP application. Read the Console Commands article to learn 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. require __DIR__.'/vendor/autoload.php';
  5. use Symfony\Component\Console\Application;
  6. $application = new Application();
  7. // ... register commands
  8. $application->run();

Then, you can register the commands using add():

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

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

Learn more

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.