How to add CLI commands
Overview of adding CLI commands
Magento enables your component to add commands to our Symfony-like command-line interface (CLI).
About the Magento CLI
Magento has one command-line interface that performs both installation and configuration tasks:
<your Magento install dir>/bin/magento. The new interface performs multiple tasks, including:
- Installing Magento (and related tasks such as creating or updating the database schema, creating the deployment configuration, and so on).
- Clearing the cache.
- Managing indexes, including reindexing.
- Creating translation dictionaries and translation packages.
- Generating non-existent classes such as factories and interceptors for plug-ins, generating the dependency injection configuration for the object manager.
- Deploying static view files.
- Creating CSS from LESS.
- A single command (
php <your Magento install dir>/bin/magento list) lists all available installation and configuration commands.
- Consistent user interface based on Symfony.
- The CLI is extensible so third party developers can “plug in” to it. This has the additional benefit of eliminating users’ learning curve.
- Commands for disabled modules do not display.
Before you begin, make sure you understand the following:
- All Magento command-line interface (CLI) commands rely on the Magento application and must have access to its context, dependency injections, plug-ins, and so on.
- All CLI commands should be implemented in the scope of your module and should depend on the module’s status.
- Your command can use the Object Manager and Magento dependency injection features; for example, it can use constructor dependency injection.
You must register your commands as discussed in any of the following sections:
Add CLI commands using dependency injection
The Magento 2 sample modules provide a demonstration of many programming techniques, including adding a CLI command using dependency injection
. Look at the
sample-module-command for an example. The module’s README.md discusses how to install it.
Following is a summary of the process:
Create a Command class (the recommended location is
<your component root dir>/Console/Command).
- Declare your Command class in
Magento\Framework\Console\CommandListInterfaceusing dependency injection (
<your component root dir>/etc/di.xml).
Clean the cache and compiled code directories:
cd <your Magento install dir>/var rm -rf cache/* page_cache/* di/* generation/*
Add CLI commands using the Composer autoloader
To be added at a later time.