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 中html

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...web

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>
相關文章
相關標籤/搜索