Magento添加CLI命令.docx

上传人:b****0 文档编号:9916383 上传时间:2023-05-22 格式:DOCX 页数:10 大小:19.59KB
下载 相关 举报
Magento添加CLI命令.docx_第1页
第1页 / 共10页
Magento添加CLI命令.docx_第2页
第2页 / 共10页
Magento添加CLI命令.docx_第3页
第3页 / 共10页
Magento添加CLI命令.docx_第4页
第4页 / 共10页
Magento添加CLI命令.docx_第5页
第5页 / 共10页
Magento添加CLI命令.docx_第6页
第6页 / 共10页
Magento添加CLI命令.docx_第7页
第7页 / 共10页
Magento添加CLI命令.docx_第8页
第8页 / 共10页
Magento添加CLI命令.docx_第9页
第9页 / 共10页
Magento添加CLI命令.docx_第10页
第10页 / 共10页
亲,该文档总共10页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

Magento添加CLI命令.docx

《Magento添加CLI命令.docx》由会员分享,可在线阅读,更多相关《Magento添加CLI命令.docx(10页珍藏版)》请在冰点文库上搜索。

Magento添加CLI命令.docx

Magento添加CLI命令

添加CLI命令

您的模块可以选择使用Magento2的基于Symfony的命令行界面(CLI)为用户提供与之交互的命令。

1.命令命名指南

1.1.命名指南概述

Magento2引入了新的命令行界面(CLI),使组件开发人员可以插入模块提供的命令。

作为扩展开发人员,您现在可以为Magento应用程序创建和分发自己的命令。

但是对于任何实现,遵循一些常规约定以使您的命令与其他开发人员的命令保持一致也很重要。

以这种方式保持一致会减少用户的学习曲线。

本主题讨论我们推荐的命名约定。

1.2.命令名

命令名称是命令的一部分,它在非常高的级别上定义命令的行为。

在命令中,它紧随命令名称之后。

例如,在bin/magentosetup:

upgrade中,bin/magento是命令的名称,而setup:

upgrade是命令的名称。

如果您方便使用Magento安装,请输入以下内容以显示当前命令列表:

$bin/magentolist

格式:

 group:

[subject:

]action

✧group

group代表一组相关命令。

组中的命令显示在列表中,从而使用户更容易找到所需的命令。

要查找命令的组名,请想象可以使用该命令的主题区域。

主题区域可以是以下任意一个:

∙Domain区域(例如,module用于带模块的操作,info用于提供某些信息的命令)

∙Workflow区域(例如,admin对于管理员可以使用的命令,dev对于开发人员使用的命令)

✧subject

subject是行动的主题。

该主题是可选的,但对于定义可用于同一对象的命令集很有用。

如果主题由复合词表示,请使用破折号或连字符将这些词分开。

✧action

action 是命令执行的操作。

✧例子

//generalcommands:

justagroupandanaction

bin/magentosetup:

install

bin/magentomodule:

status

//setofcommandswithasubject

bin/magentosetup:

config:

set

bin/magentosetup:

config:

delete

bin/magentosetup:

db-schema:

upgrade

bin/magentosetup:

db-data:

upgrade

db-schema和db-data是复合词的示例。

1.3.命令选项和参数

选项和参数跟随命令名称并修改命令的行为。

例如,在bin/magentomodule:

disable--forceMagento_Catalog中,--force 选项和Magento_Catalog 参数绕过限制并指定要禁用的特定模块;在这种情况下,无论对其他模块的依赖性如何。

选项和参数创建不同的用户体验。

作为开发人员,您可以选择哪种输入类型更适合您的特定情况。

1.3.1.命令参数

参数是用户以指定顺序传递的值。

参数名称对用户不可见。

格式:

单个单词或复合单词,用破折号或连字符分隔

例:

$bin/magentodev:

theme:

createfrontendvendorthemename

where:

frontend 是主题领域的论点

vendor 是供应商的论点

themename是主题名称参数

当需要用户提供所需数据时,请使用参数。

我们建议尽可能少的参数(不超过三个),这样用户就不会混淆其顺序。

为了使用户更简单,我们建议以下操作:

∙多次运行CLI以提供多个相似的值,而不是一次使用20个值运行一次

∙如有可能,请为必需的参数使用默认值。

然后,您可以使用选项而不是参数来最小化用户必须输入的所需数据量。

∙用选项替换参数:

为选项命名,这样用户可以以任何顺序提供它们。

这需要额外的数据验证(默认情况下,所有选项都是可选的)。

1.3.2.命令选项

选项是名称-值对。

输入值的顺序无关紧要。

一个选项可以有值或没有值。

不需要值的选项表示标志(yes或no)。

一个选项也可以使用一个字母的快捷方式来代替其全名。

启用常用选项的快捷方式,或者确定快捷方式的含义是否容易。

通常,启用类似于广泛使用的命令中的选项的快捷方式是有意义的(例如,-f表示—force,-v表示—verbose,-h表示—help)。

格式:

单个单词或复合单词,用破折号或连字符分隔。

例如,

$bin/magentodev:

theme:

create--parent=Magento/lumafrontendarg1arg2

$bin/magentodev:

theme:

create-p=Magento/lumafrontendvendorthemename

$bin/magentodev:

theme:

create--extend-from=Magento/lumafrontendvendorthemename

$bin/magentomodule:

disable-fMagento_Cms

Where:

--parent 是一个指定父主题的选项

-p 是--parent的快捷方式 

-f 是非值选项--force的快捷方式 

arg1,arg2,frontend,vendor和themename是参数(见命令选项和参数)。

使用以下选项:

∙Optionaldata

∙Requireddatathathasadefaultvalue

例:

//correct

bin/magentodev:

theme:

create--extend-from=Magento/lumafrontendFoobar

bin/magentomodule:

disable--forceMagento_Catalog

bin/magentomodule:

disable-fMagento_Catalog

//incorrect

bin/magentomodule:

disable--force=1Magento_Catalog

bin/magentomodule:

disable-f=yesMagento_Catalog

1.4.避免命名冲突的建议

为避免将您的命令命名为与另一个命令相同,我们建议:

∙在为命令选择名称之前,请先查看Magento市场中的其他扩展。

通过提前计划,您可以完全避免命名冲突。

∙限制命令名称以唯一名称(例如供应商名称)开头。

该命令的可用性取决于您为供应商名称选择的内容。

例如,myname:

dev:

theme:

create它并不明显,很难记住。

供应商名称不必以命令名称开头;它可能在中间。

这样,相关的命令被组合在一起。

例子:

dev:

myname:

theme:

create

dev:

myname:

theme:

delete

2.如何添加CLI命令

2.1.添加CLI命令概述

Magento使您的组件可以向类似于Symfony的命令行界面(CLI)添加命令。

2.1.1.关于MagentoCLI

Magento具有一个命令行界面,可以执行安装和配置任务:

/bin/magento。

新界面执行多项任务,包括:

∙安装Magento(以及相关任务,例如创建或更新数据库架构,创建部署配置,等等)。

∙清除缓存。

∙管理索引,包括重新索引。

∙创建翻译词典和翻译包。

∙为插件生成不存在的类(例如工厂和拦截器),从而为对象管理器生成依赖项注入配置。

∙部署静态视图文件。

∙从Less创建CSS。

其他福利:

∙单个命令(/bin/magentolist)列出了所有可用的安装和配置命令。

∙基于Symfony的一致的用户界面。

∙CLI是可扩展的,因此第三方开发人员可以“插入”它。

这具有消除用户学习曲线的额外好处。

∙不显示禁用模块的命令。

2.1.2.先决条件

在开始之前,请确保您了解以下内容:

∙所有Magento命令行界面(CLI)命令都依赖于Magento应用程序,并且必须有权访问其上下文,依赖项注入,插件等。

∙所有CLI命令都应在模块范围内实施,并应取决于模块的状态。

∙您的命令可以使用对象管理器和Magento依赖项注入功能;例如,它可以使用构造函数依赖注入。

∙您的命令应具有一个唯一的name,在configure()Command类的方法中定义:

1

2

3

4

5

6

7

8

protectedfunctionconfigure(){

$this->setName('my:

first:

command');

$this->setDescription('Thisismyfirstconsolecommand.');

parent:

:

configure();}...

或在di.xml文件中:

1

2

3

4

5

6

7

8

9

10

xsi="http:

//www.w3.org/2001/XMLSchema-instance"xsi:

noNamespaceSchemaLocation="urn:

magento:

framework:

ObjectManager/etc/config.xsd">

...

--configurethecommandnameviaconstructor$nameargument-->

type="string">my:

first:

command

...

或在__construct方法中(声明与相似di.xml):

1

2

3

4

publicfunction__construct(){

parent:

:

__construct('my:

first:

command');}

否则,Symfony框架将返回Thecommanddefinedin""cannothaveanemptyname.错误。

2.2.使用依赖项注入添加CLI命令

Magento2示例模块提供了许多编程技术的演示,包括使用依赖项注入添加CLI命令。

看sample-module-command一个例子。

该模块的README.md讨论了如何安装它。

以下是该过程的摘要:

1.创建一个Command类(建议位置为/Console/Command)。

参见/Console/Command/StoreListCommand.php

例如。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

php

namespaceMagento\CommandExample\Console\Command;

useSymfony\Component\Console\Command\Command;

useSymfony\Component\Console\Input\InputInterface;

useSymfony\Component\Console\Input\InputOption;

useSymfony\Component\Console\Output\OutputInterface;

/**

*ClassSomeCommand

*/

classSomeCommandextendsCommand

{

constNAME='name';

/**

*@inheritDoc

*/

protectedfunctionconfigure()

{

$this->setName('my:

first:

command');

$this->setDescription('Thisismyfirstconsolecommand.');

$this->addOption(

self:

:

NAME,

null,

InputOption:

:

VALUE_REQUIRED,

'Name'

);

parent:

:

configure();

}

/**

*Executethecommand

*

*@paramInputInterface$input

*@paramOutputInterface$output

*

*@returnnull|int

*/

protectedfunctionexecute(InputInterface$input,OutputInterface$output)

{

if($name=$input->getOption(self:

:

NAME)){

$output->writeln('Providednameis`'.$name.'`');

}

$output->writeln('SuccessMessage.');

$output->writeln('Anerrorencountered.');

$output->writeln('SomeComment.');

}

}

使用标签设置输出文本的样式。

有关样式的更多信息,请参见Symfony文档。

2.在Magento\Framework\Console\CommandListInterface声明您的Command类,并使用依赖项注入配置命令名称(/etc/di.xml):

1

2

3

4

5

6

7

8

9

10

11

xsi="http:

//www.w3.org/2001/XMLSchema-instance"xsi:

noNamespaceSchemaLocation="urn:

magento:

framework:

ObjectManager/etc/config.xsd">

...

type="array">

type="object">Magento\CommandExample\Console\Command\SomeCommand

...

3.清理缓存:

$bin/magentocache:

clean

4.重新生成代码:

$bin/magentosetup:

di:

compile

2.2.1.结果

结果,可以使用my:

first:

command接受--name参数的新命令。

$bin/magentomy:

first:

command--name'John'

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 经管营销 > 经济市场

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2