Magento2开发教程十一-XML配置说明

di.xml 指定或重写Proxy

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <!-- for = 受影响的原始类或接口 -->
    <!-- type = 所依赖的Proxy -->
   <preference for="Magento\Contact\Controller\Index\Post" type="Infinity\Contact\Controller\Index\Post" />
</config>

ps: 重写 block 时如涉及读取组件模板,可能也需要把 phtml 复制到当前 module 中

di.xml 指定或重写某个类中构造函数入参所依赖Proxy

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <!-- name = 受影响的原始类 -->
    <type name="[Vendor]\[Module]\Xxx">
        <arguments>
            <!-- name = __construct函数的参数名 -->
            <!-- text() = 指定所依赖的Proxy -->
            <argument name="arg1" xsi:type="object">[Vendor]\[Module]\Xxxxx\Proxy</argument>
        </arguments>
    </type>
</config>

di.xml 声明plugin,用于修改目标类若干个method的入参或者返回结果

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <!-- name = 需要执行插件的类。包含空间名 -->
    <type name="Magento\Customer\Model\Resource\Visitor">
        <!-- name = plugin name, 全局唯一 -->
        <!-- sortOrder = 运行顺序 -->
        <!-- disabled = 是否关闭plugin -->
        <plugin name="catalogLog"
            type="Magento\Catalog\Model\Plugin\Log"
            sortOrder="1"
            disabled="true" />
    </type>
</config>

http://devdocs.magento.com/gu…

layout

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <!-- 通过layout可以修改title -->
        <referenceBlock name="page.main.title">
            <action method="setPageTitle">
                <argument translate="true" name="title" xsi:type="string">Page Title</argument>
            </action>
        </referenceBlock>
        <!-- container将声明容器,block必须放在container内 -->
        <container name="div.sidebar.additional" htmlTag="div" htmlClass="sidebar sidebar-additional" after="div.sidebar.main">
            <container name="sidebar.additional" as="sidebar_additional" label="Sidebar Additional"/>
        </container>
        <!-- referenceContainer对已存在容器进行改造 -->
        <referenceContainer name="sidebar.additional">
            <!-- block将声明区块 -->
            <block class="Magento\Framework\View\Element\Template" name="catalog.compare.sidebar"
                   template="Magento_Catalog::product/compare/sidebar.phtml">
                <!-- action将直接调用block class中的方法 -->
                <action method="setText">
                    <argument name="text" translate="true" xsi:type="string">Text</argument>
                </action>
                <!-- arguments是block class的属性列表,从于从XML传递参数到class -->
                <arguments>
                    <!-- cache生命周期 -->
                    <argument name="cache_lifetime" xsi:type="number">84600</argument>
                </arguments>
            </block>
            <!-- referenceBlock对已存在block进行改造,例如删除它,或修改arguments -->
            <referenceBlock name="catalog.compare.sidebar" remove="true" />
            <!-- move移动元素 -->
            <move element="name.of.an.element" destination="name.of.destination.element" as="new_alias" after="name.of.element.after" before="name.of.element.before"/>
        </referenceContainer>
    </body>
</page>

webapi.xml 声明webapi

<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">
    <!-- url = 外部访问的URL -->
    <!-- method = 请求方式 (GET,POST,PUT,DELETE) -->
    <route url="/V1/customerGroups/:id" method="GET">
        <service class="Magento\Customer\Api\GroupRepositoryInterface" method="getById"/>
        <!-- ACL资源,用于控制API的访问权 -->
        <resources>
            <resource ref="Magento_Customer::group"/>
        </resources>
    </route>
</routes>

events.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="controller_action_predispatch">
        <observer name="infinity_autocoupon_controller_action_predispatch" instance="Infinity\AutoCoupon\Observer\Predispatch" />
    </event>
</config>

相关文章

0 0 投票数
文章评分
订阅评论
提醒
0 评论
内联反馈
查看所有评论