Home / Blog / Magento CLI course for free!
Magento CLI course for free!

Magento CLI course for free!

  • Anna KoshevaAnna Kosheva
  • April 20, 2022

    April 20, 2022

As you know, Magento 2 Open Source is a modern PHP-based eсommerce framework. 

And as with every framework, it provides a command-line interface for performing different operations with the system.

It includes 

  • the installation of a website
  • enabling additional plugins
  • modifying data
  • performing maintenance activities such as caching security performance-related commands
  • and much more…

Magento 2 Open Source includes a so-called Magento CLI that stands for Magento Command Line Interface that ships together with Magento 2 Open Source.

Following is a summary of the process

  1. Create a Command class (the recommended location is <your component root dir>/Console/Command). See <Magento_Store_module_dir>/Console/Command/StoreListCommand.php for example.
<?php
    namespace Magento\CommandExample\Console\Command;
    use Symfony\Component\Console\Command\Command;
    use Symfony\Component\Console\Input\InputInterface;
    use Symfony\Component\Console\Input\InputOption;
    use Symfony\Component\Console\Output\OutputInterface;

    /**
     * Class SomeCommand
     */
    class SomeCommand extends Command
    {
        private const NAME = 'name';

        /**
         * @inheritDoc
         */
        protected function configure()
        {
            $this->setName('my:first:command');
            $this->setDescription('This is my first console command.');
            $this->addOption(
                self::NAME,
                null,
                InputOption::VALUE_REQUIRED,
                'Name'
            );

            parent::configure();
        }

        /**
         * Execute the command
         *
         * @param InputInterface $input
         * @param OutputInterface $output
         *
         * @return null|int
         */
        protected function execute(InputInterface $input, OutputInterface $output)
        {
            if ($name = $input->getOption(self::NAME)) {
                $output->writeln('<info>Provided name is `' . $name . '`</info>');
            }

            $output->writeln('<info>Success Message.</info>');
            $output->writeln('<error>An error encountered.</error>');
            $output->writeln('<comment>Some Comment.</comment>');
        }
    }

2. Declare your Command class in Magento\Framework\Console\CommandListInterface and configure the command name using dependency injection (<your component root dir>/etc/di.xml):

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    ...
    <type name="Magento\Framework\Console\CommandListInterface">
        <arguments>
            <argument name="commands" xsi:type="array">
                <item name="commandexample_somecommand" xsi:type="object">Magento\CommandExample\Console\Command\SomeCommand</item>
            </argument>
        </arguments>
    </type>
    ...
</config>

3. Clean the cache:

$ bin/magento cache:clean

4. Regenerate the code:

$ bin/magento setup:di:compile

As a result, the new command my:first:command that accepts a –name parameter is ready to use.

$ bin/magento my:first:command –name ‘John’

Congratulations! You’re the winner!

But there are other tasks for CLI. Also you should know about:

  • Changing Magento 2 CLI File Permissions
  • The CLI commands difference between installed and non-installed Magento 2
  • The Admin User Create command
  • The Admin URI Command
  • The Admin User Unlock Command
  • The deploy:mode:show Command

Good news! You can learn it for free!

Mage Mastery has a free course: Magento 2 Command Line Interface (CLI) for Beginners. 

What about conditions?

Absolutely Free

The course materials are absolutely free and you have 24/7 access to watch videos on your own pace.

Knowledge Gain

Each lesson content has been carefully polished and recorded so you can get most out with Mage Mastery

Join now!

  • Anna KoshevaAnna Kosheva
  • April 20, 2022

    April 20, 2022

Sign up and don't miss our awesome Mage Mastery lessons and updates!

Related Posts
Magento 2 Payment Gateway API Classes

Magento 2 Payment Gateway API classes and interfaces explained with examples and tips on how to implement a custom payment method.

eCommerce Digest #46

eCommerce Digest #46

editAnna Kosheva
edit December 13, 2022

What eCommerce trends will 2023 bring us? Learn more about them and eCommerce future in the first digest in 2023!

Magento Tech Digest #219

Magento Tech Digest #219

editAnna Kosheva
edit December 13, 2022

How to custom UI Component Filter? How to show payment methods for particular categories in Magento 2? Learn more here.

If you like this blog post, you may want to watch
Beginner
 
Max Pronko
Web Icon13 lessons
Clock Icon1 hour 57 mins
In this free course, you will learn Magento 2 development concepts and gain knowledge of building a fully functional Magento 2 Blog module. The online course will help you develop the necessary skills to become an Adobe Commerce Developer Expert.
Intermediate
 
Max Pronko
Web Icon20 lessons
Clock Icon15 hours 00 mins
Adobe Certified Professional — Magento Commerce Developer Study Group is an 10-week (1.5 hours/week) instructor-led study group. This study group will help PHP and JavaScript developers evaluate their readiness and prepare for the Adobe Certified Professional — Magento Commerce Developer exam.
2023 © Made with ❤ by Max Pronko