Magento is a powerful CMS that offers CLI (Command Line Interface)/SSH access. Many new Magento 2 commands have been added in bin/magento, to perform multiple operations in a Magento application just through the Magento CLI.
What is Magento CLI and How to Access Files Through it
CLI is a text-based user interface used to run programs, manage files, and interact with computers or applications. If your Magento store is hosted on a server with an operating system like Linux, then commands (text based) would be used to interact with the Magento application and perform certain tasks.
How to Access Files Through Magento CLI
You can access your application from the server based on your operating system..
The general directory structure for Magento is:
cd <magento_root>/bin
For example:
Ubuntu: cd /var/www/magento2/bin
CentOS: cd /var/www/html/magento2/bin
Since my store is hosted on Cloudways so this is how I will access Magento CLI:
- Login to your Server and access SSH
- Enter your Credentials
- Access your Magento application and it’s public folder
Now, you are in the public directory of the Magento application and can run any bin/magento command.
List of Useful Magento 2 Commands And How to Use Them
The Magento 2 CLI commands structure is more similar to this one:
Command Structure: [option] [arguments]
Magento Commands can be used in any of these 3 ways:
- php magento <command>
- ./magento <command>
- magento <command>
Example: php bin/magento indexer:reindex
Here are some of the useful and common Magento commands for managing the Magento application.
Magento 2 Cache Commands
As the name suggests, this Magento 2 command can manipulate Magento’s caching option. You can enable, disable, clean, flush or check status of Magento cache with these commands:
- Command: magento cache:{enable/disable/clean/flush/status}
Example
To list cache types and their status
magento cache:status
To enable all caches OR any specific cache type
magento cache:enable
To disable all caches OR any specific cache type
magento cache:disable
It would delete cache types linked with Magento only
magento cache:clean
Deletes all cache of the same storage
magento cache:flush
Magento 2 Indexer Commands
The indexer is a widely used option in Magento 2. Previously there wasn’t any method to update indexing manually, but now it can also be done through Magento’s Admin panel. Besides reindexing, you have the options to set indexing mode and to check the status of indexer via Magento CLI.
- Command: magento indexer: {status/show-mode/set-mode/reindex/info/reset/show-dimensions-mode/set-dimensions-mode}
Example
Show list of all indexers
magento indexer:info
View status of all indexers or specific indexer only
magento indexer:status
For changing indexer mode
magento indexer:set‐mode
To reindex all the indexer or specific indexer only
magento indexer:reindex
To invalidate all the indexer or specific indexer only
magento indexer:reset
For viewing indexer current configuration
magento indexer:show-mode
Magento 2 Admin Commands
With this Magento command, you can create a new admin user, edit or unlock the user that is already created through Magento CLI.
Command:
- bin/magento admin:user:{create/unlock}
- bin/magento admin:user:create [–<parameter_name>=<value>, …]
Example:
magento admin:user:create ‐‐admin‐user="example" ‐‐admin‐password="example123" ‐‐admin‐email="[email protected]" ‐‐admin‐firstname="example" ‐‐admin‐lastname="example"
Magento 2 Module Commands
This Magento command can be used to enable, disable, uninstall or check the status of all the installed modules through Magento CLI. Disabling or uninstalling a module will fail with this Magento 2 command if there is any dependency of that module.
- Command: magento module:user:{status/enable/disable/uninstall}
Example
Check status of Magento modules
magento module:status
To enable specific Magento module
magento module:enable
To disable specific Magento module
magento module:disable
To uninstall specific Magento module
magento module:uninstall
Magento 2 Cron Command
With this Magento 2 command, we can execute the scheduled Magento Cron jobs manually. You can put this command in the scheduler, so the application’s task can be executed in a timely manner.
Command
magento cron:run
Magento 2 Setup Commands
There are plenty of things that you can do with these Magento commands.
-
Backup
We can backup our application’s code, database, and media files through this Magento CLI command. Backup can be found in the var/backup/directory of your web root.
Command
magento setup:backup ‐‐code ‐‐media ‐‐db
-
Config:set
This option can be used to make configuration changes in Magento. The changes involve DB parameters, admin URL, etc. You can see your provided values reflected in the app/etc/env.php file.
Command
magento setup:config:set ‐‐backend‐frontname="admin" ‐‐db‐host="localhost" ‐‐db‐name="example" ‐‐db‐user="example" ‐‐db‐password="example"
-
Other Magento Setup Commands
It Compiles all non-existent proxies, factories, pre-compiled class definitions, inheritance information, and plug-in definitions.
Command
magento setup:di:compile
For deploying static view files
- Command bin/magento setup:static-content:deploy {[<languages>] [-t|–theme[=”<theme>”]] [–exclude-theme[=”<theme>”]] [-l|–language[=”<language>”]] [–exclude-language[=”<language>”]] [-a|–area[=”<area>”]] [–exclude-area[=”<area>”]] [-j|–jobs[=”<number>”]] [–no-javascript] [–no-css] [–no-less] [–no-images] [–no-fonts] [–no-html] [–no-misc] [–no-html-minify] [–no-parent] [-f|–force]}
Example
magento setup:static-content:deploy
bin/magento setup:static-content:deploy en_US --exclude-theme Magento/testtheme --no-html-minify
To generate data for performance testing
Command:
- magento setup:perf:generate-fixtures
- bin/magento setup:perf:generate-fixtures {path to profile}
Example
bin/magento setup:perf:generate-fixtures /var/www/html/magento2/setup/performance-toolkit/profiles/ce/test.xml
Other Useful Magento 2 Commands
To generate translation dictionary
Command:
- magento i18n:{collect-phrases/pack/uninstall}
- bin/magento i18n:collect-phrases [-o|–output=”<csv file path and name>”] [-m|–magento] <path to directory to translate>
- bin/magento i18n:pack [-m|–mode={merge|replace}] [-d|–allow-duplicates] <source> <locale>
Example
magento i18n:collect-phrases
To create CSS from LESS
Command:
- magento dev:source-theme:deploy
- bin/magento dev:source-theme:deploy [–type=”…”] [–locale=”…”] [–area=”…”] [–theme=”…”] [file1] … [fileN]
Example
bin/magento dev:source-theme:deploy --type="less" --locale="en_US" --area="frontend" --theme="VendorName/themeName" css/styles-l
Runs the specified automated test
- Command: bin/magento dev:tests:run <test>
Example
bin/magento dev:tests:run integration
Shows list of test
Example
bin/magento dev:tests:run --help
To update the layout XML file
- Command: bin/magento dev:xml:convert
To install sample data on Magento application
- Command: magento sampledata:install
For managing backend configuration
- Command: magento config:{set/sensitive:set/show/}
To check complete list of Magento commands
- Command: bin/magento list
To get help for any particular command
- Command: bin/magento help <command>
Example
bin/magento help cache:enable
For more details about Magento 2 commands, check out their official guide.
Fairly speaking, we have more freedom working with Magento CLI on these new versions of Magento. With this flexibility, there is little need of using Magento admin for store management.
With CLI commands you can also install Magento 2 on PHP using this tutorial.
So far, we have learned about Magento CLI and how to use it’s predefined commands. Now, let’s learn how to create your custom command and how to use it.
The great thing is that Magento developers can now create custom Magento 2 CLI commands for their modules. While this is great, developers should always make sure that they observe the best code writing standards as defined by the Magento dev community.
How to Add Custom Magento 2 Commands
The process to create Magento 2 commands is simple. In this tutorial, I will not go into the details of creating a Magento 2 module.
Just create a simple module Cloudways_Commandline with etc/module.xml and registration.php files included in the root of the module’s directory.
Now, create a di.xml file in the etc directory of your module. Insert the following code in the file:
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <type name="Magento\Framework\Console\CommandList"> <arguments> <argument name="commands" xsi:type="array"> <item name="CloudwaysHicustomer" xsi:type="object">Cloudways\Commandline\Console\Hicustomer</item> </argument> </arguments> </type> </config>
As you can see, I have added an entry in the di.xml file. The <type> tag is pointing to the Magento\Framework\Console\CommandList class. Furthermore, it has the <argument> named commands, and the <item> tag. It seems like Magento 2 does some magic by sending the argument to the CommandList class. That is how dependency injection works – injecting the Command object (defined in the <item> tag) into an array of all commands.
I have also declared a class Cloudways\Commandline\Console\Hicustomer in the <item> tag. This class will set the name, and description of the command using the configure() method. The class will also define the execute() method for the command.
Create Class for Magento 2 Console Command
The next step is the creation of the class file Hicustomer.php (as defined in di.xml), in the Console directory of the module and add the following code to it:
<?php namespace Cloudways\Commandline\Console; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; class Hicustomer extends Command { protected function configure() { $this->setName('cloudways:hicustomer'); $this->setDescription('Demo command line'); parent::configure(); } protected function execute(InputInterface $input, OutputInterface $output) { $output->writeln("Hi Customer"); } }
The Hicustomer class has two methods. The configure() method is used to set initial configuration (such as name, description, and arguments) for the command. The execute() method will contain the logic that would be executed when the Magento CLI command is called in the console.
Run the Added Magento 2 CLI Command
Once the class has been declared, enable the module, run the setup upgrade command, and flush Magento 2 cache. Now, type the following CLI command to see the list of all available Magento 2 commands:
php bin/magento list
You will notice that the custom Magento CLI command is also on the list.
Now, run the php bin/magento cloudways:hicustomer command to see the desired result.
Add Argument to Custom Magento 2 Command
This was just a basic Magento CLI command. To make things interesting, I will now demonstrate how to add an argument to the custom Magento command. For this, replace the current contents of the class file Hicustomer.php with the following.
<?php namespace Cloudways\Commandline\Console; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Input\InputOption; class Hicustomer extends Command { const CUSTOMERNAME = 'Name'; protected function configure() { $commandoptions = [new InputOption(self::CUSTOMERNAME, null, InputOption::VALUE_REQUIRED, 'Name')]; $this->setName('cloudways:hicustomer'); $this->setDescription('Demo command line'); $this->setDefinition($commandoptions); parent::configure(); } protected function execute(InputInterface $input, OutputInterface $output) { if ($customername = $input->getOption(self::CUSTOMERNAME)) { $output->writeln("Hi ".$customername); } else { $output->writeln("Hi Customer"); } } }
As you can see, I have created and added the Name argument for the custom Magento command in the configure() method using $this->setDefinition($commandoptions); function and get it in the execute() method.
Once again, run the setup upgrade command and flush the Magento 2 cache. Invoke the custom Magento CLI command by php bin/magento cloudways:hicustomer –Name=”Fayyaz” and it will show “Hi Fayyaz” as an output.
Final Words!
In this tutorial, I have covered the basics to create Magento CLI commands, and explored the best way of adding a new CLI Magento 2 Command. I hope you have found this tutorial useful. If you have any feedback, feel free to share it in the comments section below.
Customer Review at
“Great speed, features, knowledgebase, dashboard, UX and fast, expert support. Very happy!”
Stefan [Management Consultant]
Abdur Rahman
Abdur Rahman is the Magento whizz at Cloudways. He is growth ambitious, and aims to learn & share information about Ecommerce & Magento Development through practice and experimentation. He loves to travel and explore new ideas whenever he finds time. Get in touch with him at [email protected]