当前位置: 技术文章>> 在Magento2中添加新的控制台命令Command

文章标题:在Magento2中添加新的控制台命令Command
  • 文章分类: Magento
  • 12993 阅读
系统学习magento二次开发,推荐小册:《Magento中文全栈二次开发 》

本小册面向Magento2以上版本,书代码及示例兼容magento2.0-2.4版本。涵盖了magento前端开发,后端开发,magento2主题,magento2重写,magento2 layout,magento2控制器,magento2 block等相关内容,带领您成为magento开发技术专家。


还记得那个时候你想创建一个Magento shell或php脚本来执行一些操作,但不知道把它放在哪里吗?

还记得根 Magento 安装中的 /shell 文件夹(或根文件夹,就此而言)吗?

还记得它曾经是如何混合 50 个 shell 和 php 脚本的,光看它们你就头疼吗?

Pepperidge Farm记得。

那是美好的旧时光,像所有美好的时光一样,它们现在已经过去。街区上有一个新的孩子,更强大,更酷,最好的部分是 - 它被集成到Magento 2中。

当然,我们正在谈论Magento 2的新功能,即其控制台组件。

根文件夹现在包含一个新目录 – ,其中有一个“magento”脚本,用于启动控制台组件。通过在终端中输入,我们会收到许多可以运行的命令,例如:/binbin/magento

创建管理员用户(管理员:用户:创建)

清除、禁用、启用缓存(缓存:清理、缓存:启用、缓存:禁用等)

运行 cron (cron:run)

启用和禁用模块(模块:启用,模块:禁用)

检查索引器状态,并在需要时重新索引(索引器:信息、索引器:重新索引等)

等等

与Magento 1相比,这是一个重大改进,就像Magento所做的一切一样(尽管这个是从Symfony借来的),它是高度可定制和可扩展的。我们可以相当轻松地添加自己的命令,这正是我们将在本文中要做的。

我们不会详细介绍在Magento 2中创建新模块,因为已经有很多教程可以遵循。

为了添加新命令,我们只需要执行几个步骤。首先,在模块的 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="hello_world" xsi:type="object">Inchoo\Console\Console\Command\HelloWorldCommand</item>
                </argument>
            </arguments>
        </type>
    </config>

我们在 di.xml 文件中添加了一个新条目。它有什么作用?如果我们检查类型标签及其名称,我们会看到它指向我们。此外,它还具有一些名为“命令”的参数和一个项目标签。看起来很奇怪。让我们去上课,看看有什么大惊小怪的:"Magento\Framework\Console\CommandList"CommandList

class CommandList implements CommandListInterface
{
    /**
     * @var string[]
     */
    protected $commands;
 
    /**
     * Constructor
     *
     * @param array $commands
     */
    public function __construct(array $commands = [])
    {
        $this->commands = $commands;
    }
 
    /**
     * {@inheritdoc}
     */
    public function getCommands()
    {
        return $this->commands;
    }
}

这里没有太多事情发生。简单类,其中构造函数接收数组作为参数,并且它有一个 getter 方法和一个受保护的变量,以及。嗯..等等,构造函数正在接收数组?这看起来很熟悉。我们有一些东西提到了一个名为 .让我们再看一遍:commandscommandsdi.xmlcommands

<arguments>
    <argument name="commands" xsi:type="array">
        <item name="hello_world" xsi:type="object">Inchoo\Console\Console\Command\HelloWorldCommand</item>
    </argument>
</arguments>

有趣。我们的参数标签确实被调用,并且它的类型设置为“数组”。看起来Magento正在使用某种魔法将我们的参数发送到CommandList类。 有一个 ,这必须是它发送的东西。

如果我们检查Magento文档(是的,您没看错):di.xmlscommands<argument><item>

“参数在创建类实例期间注入到类实例中。参数名称必须与已配置类的构造函数参数相对应。

我们看到这实际上是工作中的依赖注入——将我们的 Command 对象注入到所有命令的数组中。整洁。

让我们看看这个 HelloWorldCommand 类(它位于我们模块的控制台目录中)。

class HelloWorldCommand extends Command
{
 
    protected function configure()
    {
        $this->setName('inchoo:hello_world')->setDescription('Prints hello world.');
    }
 
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $output->writeln('Hello World!');
    }
 
}

这里只有两种方法:和 。我们从父类 () 继承它们,它们允许我们设置命令。configureexecuteCommand

Configure方法用于将初始配置设置为命令,例如:名称,描述,命令行参数等。 方法是您编写逻辑的地方。

在这个例子中,我们刚刚将“Hello World!”打印到屏幕上,但希望你会有更多的想象力。Execute

要查看该命令的运行情况,请使用 bin/magento 调用它,如下所示:

bin/magento inchoo:hello_world

你会受到命令行的欢迎。什么感觉。

为了让您的生活更轻松,我们准备了一个可以安装的作曲家存储库,无需键入所有内容即可尝试该命令。

以下是说明:

将我们的模块添加为依赖项:

composer config repositories.inchoo_console vcs git@bitbucket.org:lurajcevi/inchoo_console.git

请composer下载它:

composer require inchoo/console:dev-master

如果 Composer 要求您提供一组凭据,请按照这些说明操作。

当composer施展魔法时,我们可以启用我们的模块:

bin/magento module:enable Inchoo_Console

及以后

bin/magento setup:upgrade

我们的模块已启用,我们可以使用它。由于这个想法是向控制台应用程序添加新命令,让我们看看它是否有效。再次呼叫。bin/magento

希望您会看到以下命令(以及其他所有命令):

...

inchoo

inchoo:hello_world Prints hello world.

...

让我们调用它:

bin/magento inchoo:hello_world

响应是,正如预期的那样:

Hello World!


推荐文章