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.
1
$ composer require symfony/console
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.
- 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
- Progress Indicator
- Question Helper
- Table Helper
- Tree Helper
- 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.