Magento 2:自定义模块中的system.xml

Joh*_*ohn 6 php module magento magento2

是否可以system.xml在Magento 2中为模块配置添加文件?如果是这样,怎么样?

小智 11

是的,在magento 2中,可以创建与Magento 1.x相同的系统配置文件.但它需要创建一些其他文件.

需要使用以下文件来创建它.

1) app/code/Vendor/Helloworld/etc/adminhtml/system.xml

2) app/code/Vendor/Helloworld/etc/acl.xml
Run Code Online (Sandbox Code Playgroud)

这2个文件对于创建系统配置很重要.

system.xml档案中

添加常用内容

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../Config/etc/system_file.xsd">
    <system>
        <!-- Add new Tab -->
        <tab id="vendor" translate="label" sortOrder="300">
            <label>Vendor Extension</label>
        </tab>
        <section id="helloworld" translate="label" type="text" sortOrder="140" showInDefault="1" showInWebsite="1" showInStore="1">
            <label>Helloworld</label>
            <tab>vendor</tab>
            <!-- resource tag name which we have to defined in the acl.xml -->
            <resource>Vendor_Helloworld::config_helloworld</resource>
            <group id="general" translate="label" type="text" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
                <label>General Options</label>
                <field id="active" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Enabled</label>
                    <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
                </field>
            </group>
        </section>
    </system>
</config>
Run Code Online (Sandbox Code Playgroud)

acl.xml档案中

在文件中需要写下面的内容

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Acl/etc/acl.xsd">
    <acl>
        <resources>
            <resource id="Magento_Backend::admin">
                <resource id="Magento_Backend::stores">
                    <resource id="Magento_Backend::stores_settings">
                        <resource id="Magento_Config::config">
                            <!-- this resource id we can use in system.xml for section -->
                            <resource id="Vendor_Helloworld::config_helloworld" title="Helloworld Section" sortOrder="80" />
                        </resource>
                    </resource>
                </resource>
            </resource>
        </resources>
    </acl>
</config>
Run Code Online (Sandbox Code Playgroud)

之后,清除管理员端的magento缓存和注销.然后在管理员端登录.在商店>配置中,您可以看到"供应商扩​​展"选项卡.单击此按钮可以查看详细信息.