WARNING: You are browsing the documentation for Symfony 2.7 which is not maintained anymore. Consider upgrading your projects to Symfony 4.2.

The Console Component

2.7 version

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

Alternatively, you can clone the https://github.com/symfony/console repository.

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

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
#!/usr/bin/env php
<?php
// application.php

require __DIR__.'/vendor/autoload.php';

use Symfony\Component\Console\Application;

$application = new Application();

// ... register commands

$application->run();

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

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

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

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