了解使用 di.xml 命令替换对象管理器参数
-
28-09-2020 - |
题
我正在跟进 对象管理器参数替换 教程。可能的 di.xml 之一如下所示
<type name="Pulsestorm\TutorialObjectManagerArguments\Model\Example">
<arguments>
<argument name="scaler1" xsi:type="string">bar</argument>
</arguments>
</type>
这意味着模型中 Example
的类的构造函数,替换(无论旧的)参数值 $scaler1
和 bar
.
而对于命令来说,它看起来像
<type name="Magento\Framework\Console\CommandList">
<arguments>
<argument name="commands" xsi:type="array">
<item name="testbedCommand" xsi:type="object">Pulsestorm\TutorialObjectManagerArguments\Command\Testbed</item>
</argument>
</arguments>
</type>
其中表示添加 1 个新项目(命令) CommandList
的构造函数参数 commands
(这是数组类型)。
我看不到这个名字的用途 testbedCommand
模块中的任何位置。它的用途是什么?它应该是独一无二的吗?
解决方案
Magento\Framework\Console\CommandList 中不使用项目名称。如果您查看此类,您会发现仅使用值,但数组键也可用。
如果您在模块中定义相同的类型并具有相同的参数项名称:
<type name="Magento\Framework\Console\CommandList">
<arguments>
<argument name="commands" xsi:type="array">
<item name="testbedCommand" xsi:type="object">Your\Module\Command\Testbed</item>
</argument>
</arguments>
</type>
并且您的模块遵循 Pulsestorm utorialObjectManagerArguments,然后作为参数,您将获得您的值:
<argument name="commands" xsi:type="array">
<item name="testbedCommand" xsi:type="object">Your\Module\Command\Testbed</item>
</argument>