{ Hello Magento 2 }

解决 Magento 2 应用问题,更注重深度挖掘。(ง •̀_•́)ง

0%

How to add CLI commands

app/code/VendorName/TestModule/etc/di.xml

1
2
3
4
5
6
7
8
9
10
<?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="exampleSayHello" xsi:type="object">VendorName\TestModule\Console\Sayhello</item>
</argument>
</arguments>
</type>
</config>

app/code/VendorName/TestModule/Console/Sayhello.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?php
namespace VendorName\TestModule\Console;

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

class Sayhello extends Command
{
protected function configure()
{
$this->setName('exampleGroup:exampleSubject:sayHello');
$this->setDescription('Demo command line');

parent::configure();
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$output->writeln("Hello World");
}
}

php bin/magento php bin/magento exampleGroup:exampleSubject:sayHello;

References

How to Add Command line in to Console CLI in Magento 2 Command naming guidelines