6

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

4

1 回答 1

13

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

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

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

2) app/code/Vendor/Helloworld/etc/acl.xml

这 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>

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>

之后,从管理员端清除 magento 缓存和注销。然后在管理员端登录。在商店 > 配置中,您可以看到“供应商扩​​展”选项卡。当您单击它时,您可以看到它的详细信息。

于 2015-09-16T17:07:46.473 回答