我是 Magento 2 的新手,我正在創建一個打印 hello world 的自定義 CLI 命令,但是當我看到列表時 php bin/magento list,它沒有顯示我添加的命令,而是向我拋出了這個錯誤: [Symfony\Component\Console\Exception\CommandNotFoundException] There are no commands defined in the "training" namespace. 以下是我為使 CLI 命令正常工作所做的事情,我不認為我錯過了一些東西:應用程序/代碼/SimplifiedMagento/FirstModule/Console/Command/HelloWorld.php<?phpnamespace SimplifiedMagento\FirstModule\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 configure() { $this->setName("training:hello_world"); $this->setDescription("the command prints out hello world"); parent::configure(); } public function execute(InputInterface $input, OutputInterface $output) { $output->writeln('Hello World'); }}應用程序/代碼/SimplifiedMagento/FirstModule/etc/frontend/di.xml<type name="Magento\Framework\Console\CommandList"> <arguments> <argument name="commands" xsi:type="array"> <item name="hello_world" xsi:type="object"> SimplifiedMagento\FirstModule\Console\Command\HelloWorld</item> </argument> </arguments> </type>我不確定我哪里出錯了,有人可以幫我嗎?我的命令基本上會說training:hello_world
1 回答

慕斯709654
TA貢獻1840條經驗 獲得超5個贊
經過一番研究,我發現我必須在我的 etc 文件夾中創建一個單獨的 di.xml 文件,而不是 etc/frontend/di.xml
我已經剪切了命令的 di.xml 文件的代碼,并在 etc 文件夾中創建了一個新的文件 di.xml 文件并保留它并且它可以工作。
<type name="Magento\Framework\Console\CommandList">
<arguments>
<argument name="commands" xsi:type="array">
<item name="hello_world" xsi:type="object">
SimplifiedMagento\FirstModule\Console\Command\HelloWorld</item>
</argument>
</arguments>
</type>
- 1 回答
- 0 關注
- 109 瀏覽
添加回答
舉報
0/150
提交
取消