Skip to content
  • About
    • What is Symfony?
    • Community
    • News
    • Contributing
    • Support
  • Documentation
    • Symfony Docs
    • Symfony Book
    • Screencasts
    • Symfony Bundles
    • Symfony Cloud
    • Training
  • Services
    • SensioLabs Professional services to help you with Symfony
    • Platform.sh for Symfony Best platform to deploy Symfony apps
    • SymfonyInsight Automatic quality checks for your apps
    • Symfony Certification Prove your knowledge and boost your career
    • Blackfire Profile and monitor performance of your apps
  • Other
  • Blog
  • Download
sponsored by SensioLabs
  1. Home
  2. Documentation
  3. The Console Component
  • Documentation
  • Book
  • Reference
  • Bundles
  • Cloud

Table of Contents

  • Installation
  • Creating a Console Application
  • Learn more

The Console Component

Edit this page

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
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():

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

You can also register inline commands and define their behavior thanks to the Command::setCode() method:

1
2
3
4
5
6
7
8
// ...
$application->register('generate-admin')
    ->addArgument('username', InputArgument::REQUIRED)
    ->setCode(function (InputInterface $input, OutputInterface $output): int {
        // ...

        return Command::SUCCESS;
    });

This is useful when creating a single-command application.

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

Learn more

  • Console Commands
  • Changing the Default Command
  • Understanding how Console Arguments and Options Are Handled
  • Using Events
  • Cursor Helper
  • Debug Formatter Helper
  • Formatter Helper
  • The Console Helpers
  • Process Helper
  • Progress Bar
  • Question Helper
  • Table
  • Using the Logger
  • Building a single Command Application
  • Using Console Commands, Shortcuts and Built-in Commands
  • How to Call Other Commands
  • How to Color and Style the Console Output
  • How to Call a Command from a Controller
  • How to Define Commands as Services
  • How to Hide Console Commands
  • Console Input (Arguments & Options)
  • How to Make Commands Lazily Loaded
  • Prevent Running the Same Console Command Multiple Times
  • How to Style a Console Command
  • Verbosity Levels
This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
TOC
    Version
    We stand with Ukraine.
    Version:

    The Console component is backed by

    Symfony Code Performance Profiling

    Symfony Code Performance Profiling

    Peruse our complete Symfony & PHP solutions catalog for your web development needs.

    Peruse our complete Symfony & PHP solutions catalog for your web development needs.

    Symfony footer

    ↓ Our footer now uses the colors of the Ukrainian flag because Symfony stands with the people of Ukraine.

    Avatar of Jean Pasqualini, a Symfony contributor

    Thanks Jean Pasqualini for being a Symfony contributor

    1 commit • 1 lines changed

    View all contributors that help us make Symfony

    Become a Symfony contributor

    Be an active part of the community and contribute ideas, code and bug fixes. Both experts and newcomers are welcome.

    Learn how to contribute

    Symfony™ is a trademark of Symfony SAS. All rights reserved.

    • What is Symfony?

      • Symfony at a Glance
      • Symfony Components
      • Case Studies
      • Symfony Releases
      • Security Policy
      • Logo & Screenshots
      • Trademark & Licenses
      • symfony1 Legacy
    • Learn Symfony

      • Symfony Docs
      • Symfony Book
      • Reference
      • Bundles
      • Best Practices
      • Training
      • eLearning Platform
      • Certification
    • Screencasts

      • Learn Symfony
      • Learn PHP
      • Learn JavaScript
      • Learn Drupal
      • Learn RESTful APIs
    • Community

      • SymfonyConnect
      • Support
      • How to be Involved
      • Code of Conduct
      • Events & Meetups
      • Projects using Symfony
      • Downloads Stats
      • Contributors
      • Backers
    • Blog

      • Events & Meetups
      • A week of symfony
      • Case studies
      • Cloud
      • Community
      • Conferences
      • Diversity
      • Documentation
      • Living on the edge
      • Releases
      • Security Advisories
      • SymfonyInsight
      • Twig
      • SensioLabs
    • Services

      • SensioLabs services
      • Train developers
      • Manage your project quality
      • Improve your project performance
      • Host Symfony projects

      Deployed on

    Follow Symfony

    Search by Algolia