Magento2 后台配置选项卡(system.xml)

2018年12月28日 浏览量 40 3 min read
Magento2 后台配置选项卡(system.xml)

在这篇博客中,我们将介绍如何在magento2中为您的模块设置系统配置。

magento2中的系统配置与magento1非常相似,因此您可以点击此处阅读magento中的系统配置基础知识。系统配置分为不同的部分:在magento1博客中描述的tab, section, group and field,同样的概念也用于magento2。

system.xml文件位于magento2的adminhtml /文件夹中

Magease/Hello/etc/adminhtml/system.xml

添加内容

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
<system>
<tab id="helloworld" translate="label" sortOrder="100">
<!-- add a new tab with id helloworld -->
<label>Hello World</label>
</tab>
<section id="helloworld" translate="label" sortOrder="130" showInDefault="1" showInWebsite="1" showInStore="1">
<!-- add a new section with id helloworld and for tab helloworld -->
<class>separator-top</class>
<label>Hello World Configuration</label>
<tab>helloworld</tab>
<resource>Magease_First::test_config</resource>
<group id="active_display" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="0" showInStore="0">
<!-- add a new group with id active display -->
<label>Hello World Configuration Options</label>
<field id="scope" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="0" showInStore="0">
<!-- add a new field with id scope -->
<label>Enable Helloworld Controller</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
</group>
</section>
</system>
</config>

在上面我们可以看到如何添加一个新的tab, section, group and field

在设置这些配置项的默认值之后,创建文件

Magease/Hello/etc/config.xml

并添加代码

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
<default>
<helloworld>
<active_display>
<scope>1</scope>
</active_display>
</helloworld>
</default>
</config>

还有一个重要的事情要做,在我们的system.xml中,我们有这条线

Magease_First::test_config

用于建立系统配置的ACL,稍后我们将看到这将如何工作,现在创建文件

Magease/Hello/etc/acl.xml

添加代码

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Acl/etc/acl.xsd">
<acl>
<resources>
<resource id="Magento_Adminhtml::admin">
<resource id="Magento_Adminhtml::stores">
<resource id="Magento_Adminhtml::stores_settings">
<resource id="Magento_Adminhtml::config">
<resource id="Magease_First::test_config" title="Hello World Section" />
</resource>
</resource>
</resource>
</resource>
</resources>
</acl>
</config>

现在,当您打开magento admin时,您会看到HelloWorld tab和sections
第一次您可能会收到404 Not Found错误,只需注销并再次登录即可解决此问题

如果您想探索magento2提供的其他系统配置选项,请查看模块Magento/Config/Model/Config文件,在这里您可以看到magento提供的其他系统配置选项,您还可以从核心magento模块中浏览其他system.xml文件

Previous article:
Next article:
Comments
发表评论,留下你的足迹
我们不会公开你的邮箱地址

是否允许我们在发布新内容或者进行促销活动向您发送消息?

Remind me later

Thank you! Please check your email inbox to confirm.

Oops! Notifications are disabled.

© 2014-2023 www.magease.com. All Rights Reserved. 寰云网络 版权所有    鲁ICP备 14014975号-1