Posted on

Magento插件开发

核心 : plugin 、preference 、 event


preference 覆盖类:

就是重写类,重写注入类,依赖注入。
参考:https://www.mageplaza.com/devdocs/how-use-plugin-preference-rewrite-block-model-controller-helper-magento-2.html

<preference for="Magento\Contact\Controller\Index\Post" type="ABC\Contact\Controller\Index\Post" />

plugin 过滤返回值:

类似 wordpress 的 filter,
参考:https://www.mageplaza.com/magento-2-module-development/magento-2-plugin-interceptor.html

<config>
    <type name="{ObservedType}">
        <plugin name="{pluginName}" type="{PluginClassName}" sortOrder="1" disabled="false"/>
    </type>
</config>

event事件机制:

类似wordpress的 action事件:
参考:https://www.mageplaza.com/magento-2-module-development/magento-2-events.html

<event name="mageplaza_helloworld_display_text">
        <observer name="mp_display_text" instance="Mageplaza\HelloWorld\Observer\ChangeDisplayText" />
    </event>

开发系列文章,可以参考:

https://www.mageplaza.com/magento-2-module-development/