Magento2 添加新的 CLI 命令行

2019年1月16日 浏览量 26 2 min read
Magento2 添加新的 CLI 命令行

在这篇博客中,我们将了解Magento 2的一个新特性,它的控制台组件。

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

  • 创建管理员用户(管理员:用户:创建)
  • 清除,禁用,启用缓存(缓存:干净,缓存:启用,缓存:禁用等)
  • 运行Con(Con:运行)
  • 启用和禁用模块(模块:启用,模块:禁用)
  • 检查索引器状态,并在需要时重新索引(索引器:信息、索引器:重新索引等)
  • 更多

与Magento 1相比,这是一个重大的改进,就像Magento所做的一切(尽管这个是从Symfony借来的),它是高度可定制和可扩展

的,我们可以很容易地添加我们自己的命令,这正是我们在本文中要做的。

我们不会详细介绍在如何早Magent 2中创建一个新的模块,因为之前的博客中已经介绍了。

为了增加一个新的命令,我们只需要执行几个步骤。首先,在模块中创建一个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">Magease\Console\Console\Command\HelloWorldCommand</item>
</argument>
</arguments>
</type>
</config>

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

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;
}
}

这里没什么了。简单类,构造函数接收commands数组作为参数,它有一个getter方法和一个受保护的变量,并且.. umm ..等等,构造函数正在接收一个commands数组?这看起来很熟悉。我们di.xml提到了一个名为的数组commands。让我们再看一遍:

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

有趣。di.xmls确实调用了我们的参数标记commands,并且它的类型设置为“array”。似乎Magento正在使用某种魔法将我们的参数发送到CommandList类。<argument>有一个<item>,这必须是它发送的东西。
如果我们检查Magento文档(是的,你读的是正确的):

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

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

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

class HelloWorldCommand extends Command
{
protected function configure()
{
$this->setName('magease:hello_world')->setDescription('Prints hello world.');
}

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

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

Configure方法用于将初始配置设置为命令:名称、描述、命令行参数等。

Execute方法用于将初始配置设置为命令:名称、描述、命令行参数等。

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

bin/magento magease:hello_world

你会看到欢迎字样,多么美好的感觉。

为了让您的使用更轻松,我们准备了一个可以安装的编写器存储库,可以尝试使用该命令,而不必键入所有内容。

以下是说明:

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

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

请求Composer下载它:

composer require magease/console:dev-master

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

当composer发挥他的魔力时,我们可以启用我们的模块:

bin/magento module:enable Magease_Console

然后

bin/magento setup:upgrade

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

希望你会看到下面的命令(在所有其他命令中):

...
magease
magease:hello_world Prints hello world.
...

我们来调用它:

bin/magento magease:hello_world

正如预期的那样:

Hello World!

但是,如果关于Magento开发的这个或其他任何事情让您感到困惑,我们将很乐意检查您的网站,随时与我们联系!

Previous article:
Next article:
Comments
发表评论,留下你的足迹
我们不会公开你的邮箱地址

是否允许我们在发布新内容或者进行促销活动向您发送消息?

Remind me later

Thank you! Please check your email inbox to confirm.

Oops! Notifications are disabled.

© 2014-2023 www.magease.com. All Rights Reserved. 寰云网络 版权所有    鲁ICP备 14014975号-1