Magento2 编写界面插件

use*_*069 2 plugins interface magento magento2

我正在准备 Magento 认证,在实践过程中我遇到了以下问题

您将插件声明添加到 MyCompany/MyModule/etc/di.xml

请参阅下面的代码声明

此声明将产生什么效果?

  1. 例外,因为插件不得应用于接口
  2. 由于声明中的语法错误而出现异常
  3. 该插件将被忽略,因为 ActionInterface 永远不会直接实例化
  4. 该插件将应用于 ActionInterface 的所有实现者

我确信其他 2 个属性(sortOrder 和禁用)是可选的,因此代码在语法方面是正确的,我也在本地尝试了上述代码,在 setup:upgrade 和 di:compile 时生成的语法错误没有例外

需要帮助才能在这里找到正确的答案。谢谢

<type name="Magento\Framework\App\ActionInterface"> 
<plugin name="my_plugin" type="MyCompany\MyModule\Plugin\ActionInterfacePlugin"/>
</type>
Run Code Online (Sandbox Code Playgroud)

小智 7

正确答案是“4.该插件将应用于ActionInterface的所有实现者”

在 Magento 中,控制器必须实现 ActionInterface 接口。因此,我们的插件将应用于所有实现该接口的类(即,事实上,它是所有控制器的插件)