:::: MENU ::::
Posts tagged with: cli commands

Magento 2 create custom command

Today we discuss how in Magento 2 create custom command. Sometime we need to import sample data or use the long queries to get data so for that it is better to use command line interface. Magento 2 use mostly command line interface for upgrade, reindex, static content deploy and many other command available. Here is list of default magento available command. So let’s start with our custom command.

Step 1: Define Command

For custom command keep in mind in item name qaisarSatti_helloworld first letter will your nameSpace. Now add following code to your di.xml

QaisarSatti\HelloWorld\etc\di.xml
<?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="qaisarSatti_helloworld" xsi:type="object">QaisarSatti\HelloWorld\Console\Command\HelloWorld</item>
            </argument>
        </arguments>
    </type>
</config>

Step 1: Create Command

Now Create file HelloWorld.php in following directory.

QaisarSatti\HelloWorld\Console\Command

In your configure method just set

setName(‘qaisarsatti:helloworld’) // this will be your command that you run from cli
setDescription(‘Command Description’) // add your command detail

In you execute method you add your custom logic

<?php


namespace QaisarSatti\HelloWorld\Console\Command;

use \Symfony\Component\Console\Command\Command;
use \Symfony\Component\Console\Input\InputInterface;
use \Symfony\Component\Console\Output\OutputInterface;


class HelloWorld extends Command
{
   

    public function __construct(
    ) {
        parent::__construct();
    }

   
    protected function configure()
    {
        $this->setName('qaisarsatti:helloworld')
            ->setDescription('Just Checking for command'); //add you description here

        parent::configure();
    }

   
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        try{
    //add your logic here
            $output->writeln('<info>Yahoooo command Successfully working!</info>');
        } catch (\Exception $e) {
            $output->writeln("<error>{$e->getMessage()}</error>");
        }
    }
}

Now check our command is listed or not. Just run following command

php bin/magento list

qaisarsatti
  qaisarsatti:helloworld                   Just Checking for command
command list

Now we execute the our command

php bin/magento qaisarsatti:helloworld

The output will be shown

Yahoooo command Successfully working!
run command

Now following this you can add new custom command in magento 2.

Author: Qaisar Satti
Category: Magento 2
Last Modified: July 12, 2018


Magento 2 deploy static content theme

Today we talk about how to use command line interface to do Magento 2 deploy static content theme.Doing the static content deployment is quite a headache. So today i will tell how to deploy content smartly. Magento 2 give option to deploy language example en_US, deploy area example frontend or adminhtml also the deploy the theme example blank or luma. Now we separate these with frontend and backend. This tutorial include the single theme deploy in magento 2, area base deployment example frontend or adminhtml and local base content deploy. Here is shortcut of adding the language area and theme.
-l = language
-a = area
-t = theme

Frontend theme deployment

By default you can use below command to deploy the static content. This is deploy static content for add the languages.

php bin/magento  setup:static-content:deploy

Now we add language filter so it deploy only my specific language example en_Us

php bin/magento  setup:static-content:deploy  en_US

After adding language filter we add the area specific filter so it deploy only my area language example frontend.

php bin/magento  setup:static-content:deploy  en_US -a frontend

After adding area filter we add the theme filter so it deploy only my theme example luma.

php bin/magento  setup:static-content:deploy  en_US -a frontend -t Magento/luma

Bakcend theme deployment

Using php bin/magento setup:static-content:deploy en_US after adding language filter we add the area specific filter so it deploy only my area language example backend. Now it will deploy the backend content only.

php bin/magento  setup:static-content:deploy  en_US -a adminhtml

After adding area filter we add the theme filter so it deploy only my theme example backend.

php bin/magento  setup:static-content:deploy  en_US -a adminhtml-t Magento/backend
Author: Qaisar Satti
Category: Magento 2
Last Modified: July 12, 2018


Magento 2 available commands

Today we talk about Magento 2 available commands. In this tutorail we listed all the command that we can use in magento 2. Command like deploy content, setup upgrade, indexing command, cache command and other command too. you can create custom command too with the help of Magento 2 create custom command. So let’s start with our list.

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)
cache:flush         Flushes cache storage used by cache type(s)
cache:status         Checks cache status

catalog

catalog:images:resize         Creates resized product images
catalog:product:attributes:cleanup         Removes unused product attributes.

cron

cron:run         Runs jobs by schedule

customer

customer:hash:upgrade         Upgrade customer’s hash according to the latest algorithm

deploy

deploy:mode:set         Set application mode.
deploy:mode:show         Displays current application mode.

dev

dev:source-theme:deploy         Collects and publishes source files for theme.
dev:tests:run         Runs tests
dev:urn-catalog:generate         Generates the catalog of URNs to *.xsd mappings for the IDE to highlight xml.
dev:xml:convert         Converts XML file using XSL style sheets

i18n

i18n:collect-phrases         Discovers phrases in the codebase
i18n:pack         Saves language package
i18n:uninstall         Uninstalls language packages

indexer

indexer:info         Shows allowed Indexers
indexer:reindex         Reindexes Data
indexer:reset         Resets indexer status to invalid
indexer:set-mode         Sets index mode type
indexer:show-mode         Shows Index Mode
indexer:status         Shows status of Indexer

info

info:adminuri         Displays the Magento Admin URI
info:backups:list         Prints list of available backup files
info:currency:list         Displays the list of available currencies
info:dependencies:show-framework         Shows number of dependencies on Magento framework
info:dependencies:show-modules         Shows number of dependencies between modules
info:dependencies:show-modules-circular         Shows number of circular dependencies between modules
info:language:list         Displays the list of available language locales
info:timezone:list         Displays the list of available timezones

maintenance

maintenance:allow-ips         Sets maintenance mode exempt IPs
maintenance:disable         Disables maintenance mode
maintenance:enable         Enables maintenance mode
maintenance:status         Displays maintenance mode status

module

module:disable         Disables specified modules
module:enable         Enables specified modules
module:status         Displays status of modules
module:uninstall         Uninstalls modules installed by composer

sampledata

sampledata:deploy         Deploy sample data modules
sampledata:remove         Remove all sample data packages from composer.json
sampledata:reset         Reset all sample data modules for re-installation

setup

setup:backup         Takes backup of Magento Application code base, media and database
setup:config:set         Creates or modifies the deployment configuration
setup:cron:run         Runs cron job scheduled for setup application
setup:db-data:upgrade         Installs and upgrades data in the DB
setup:db-schema:upgrade         Installs and upgrades the DB schema
setup:db:status         Checks if DB schema or data requires upgrade
setup:di:compile         Generates DI configuration and all missing classes that can be auto-generated
setup:install         Installs the Magento application
setup:performance:generate-fixtures         Generates fixtures
setup:rollback         Rolls back Magento Application codebase, media and database
setup:static-content:deploy         Deploys static view files
setup:store-config:set         Installs the store configuration
setup:uninstall         Uninstalls the Magento application
setup:upgrade         Upgrades the Magento application, DB data, and schema

theme

theme:uninstall         Uninstalls theme

Author: Qaisar Satti
Category: Magento 2
Last Modified: July 12, 2018