Some of you might already know this, but I’m a great fan of the command line. In web development, I use PHP to write CLI scripts more than often. They provide a great way if you need to perform quick tasks on the server, or if you want to perform tasks after a deployment. If you wanted to use this way of working in Magento 1, you often needed to write scripts that started with the inclusion and instantiation of the Magento framework. With Magento 2, using the CLI to the fullest has become easy as 1,2,3.
Please note that we use the boilerplate module from the article ‘Creating a module in Magento 2‘ as the entry point for this article.
What is the ‘CLI’?
CLI stands for Command Line Interface, and is a very basic way of accessing your system. Those who ever worked with DOS might recognise it (damn I’m old!). Ever worked with the Terminal or logged in with SSH? Then you’ve probably already seen the CLI for yourself. Heck, if you installed or developed with Magento 2, you might already have performed some setup:upgrade and module:enable -commands. These commands are part of Magento 2’s core CLI options. When you navigate to the bin -folder of your Magento 2 installation, add executable rights to the magento -file and execute it, you’ll see a ton of options:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
$ ./magento Magento CLI version 2.0.4 Usage: command [options] [arguments] Options: --help (-h) Display this help message --quiet (-q) Do not output any message --verbose (-v|vv|vvv) Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug --version (-V) Display this application version --ansi Force ANSI output --no-ansi Disable ANSI output --no-interaction (-n) Do not ask any interactive question Available commands: help Displays help for a command list Lists commands admin admin:user:create Creates an administrator admin:user:unlock Unlock Admin Account cache cache:clean Cleans cache type(s) cache:disable Disables cache type(s) cache:enable Enables cache type(s) ... etc ... |
The commands listed here can be executed from the terminal, so if you type:
1 |
./magento cache:clean |
… the cache gets cleared without the need to log in to the admin panel. Ideal for after a deployment or update.
Adding CLI options to your own module
So let’s say you have a module that needs to some magic stuff from the terminal. This can be a post-deployment task like deleting files or re-indexing some database tables, but also tasks that can help you locally with development, like scaffolding. How can you add these tasks? Well, to begin you have to let Magento know that you are going to use the CLI: you have to register your classes. This can be done in the di.xml -file of your module:
1 2 3 4 5 6 7 8 |
<type name="Magento\Framework\Console\CommandList"> <arguments> <argument name="commands" xsi:type="array"> <item name="gielberkers_example" xsi:type="object">Gielberkers\Example\Console\Example</item> </argument> </arguments> </type> |
And in our module, we create a folder called Console , and in that folder we place the file Example.php :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
<?php namespace Gielberkers\Example\Console; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; class Example extends Command { /** * Configure our CLI command */ protected function configure() { $this->setName('gb:example'); $this->setDescription('My Example'); $this->addArgument('argument_1'); $this->addArgument('argument_2', InputArgument::OPTIONAL); } /** * Execute * @param InputInterface $input * @param OutputInterface $output */ protected function execute(InputInterface $input, OutputInterface $output) { $argument1 = $input->getArgument('argument_1'); $argument2 = $input->getArgument('argument_2'); var_dump($argument1); var_dump($argument2); } } |
That’s it! There’s all there’s to it! Now, when you execute ./magento , you’ll see your command in the list:
1 2 |
gb gb:example My Example |
And note the use of arguments, so you can provide extra options from the command line:
1 2 3 |
$ ./magento gb:example foo bar string(3) "foo" string(3) "bar" |
This post is part of the series Magento 2 Development from Scratch.
Visitors give this article an average rating of 4.5 out of 5.
How would you rate this article?
★ ★ ★ ★ ★