System.xml 是参数配置文件,用来添加参数配置字段。如果你需要添加一些管理员可以配置的参数就要用到这个功能。可以到后台 Store -> Setting -> Configuration 查看显示方式。
创建 system.xml 有如下几步
- 1 添加 system.xml
- 2 设置默认值
- 3 清空缓存
- 4 获取配置参数
第一步 添加 System.xml
Magento 2 配置系统页面在逻辑上分为几个部分:标签页(Tabs)、区域(Sections)、组(Groups)、字段(Fields)。具体看图:
现在给我们的模块添加一些简单的配置参数。system.xml
文件在模块的 etc/adminhtml
目录,为模块新增一个标签名 “Aqrun”, 区域名“欢迎”,组中有一些简单的字段: enable 和 display_text。
文件: app/code/Aqrun/HelloWorld/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="aqrun" translate="label" sortOrder="10">
<label>Aqrun</label>
</tab>
<section id="helloworld" translate="label" sortOrder="130"
showInDefault="1" showInWebsite="1" showInStore="1">
<class>separator-top</class>
<label>欢迎</label>
<tab>aqrun</tab>
<resource>Aqrun_HelloWorld::helloworld_config</resource>
<group id="general" translate="label" type="text" sortOrder="10"
showInDefault="1" showInWebsite="0" showInStore="0">
<label>基础设置</label>
<field id="enable" translate="label" type="select"
showInDefault="1" showInWebsite="0" showInStore="0"
sortOrder="1">
<label>模块启用</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
<field id="display_text" translate="label" type="text"
showInDefault="1" showInWebsite="0" showInStore="0"
sortOrder="1">
<label>输出内容</label>
<comment>指定页面显示的内容</comment>
</field>
</group>
</section>
</system>
</config>
根据上面的代码可以看出如何添加:标签页、区域、组和字段。具体一些元素说明:
- 标签 Tab 节点有很多 区域 sections 和一些主要的属性及子节点:
- id 属性是标签的唯一标识
- sortOrder 控制标签显示顺序
- translate 属性控制哪个标题需要翻译
- label 子节点会显示为标签的标题
- 区域 section 节点和标签一样也有 id,sortOrder, translate 这些属性。还有(showInDefault, showInWebsite, showInStore)几个属性决定元素在哪个范围显示。可以在这里改变范围显示:
区域元素又会有很多组 group 和其它一些子节点:
- class 会做为 html 的 CLSS 显示,如果要改变元素的样式就会用到这个属性
- label 元素的标题文字
- tab 是标签 ID 指定这个区域显示在哪个标签页上
- resource 指定权限控制,检测后台用户是否有权限查看这个配置项
- group 组元素和区域元素一样有很多字段和属性
- fields 字段是这个文件的主要代码,可以保存设置的数据。这个元素主要是 type 属性,控制元素如何显示,可以 文本 text, 下拉选项 select, 文件 file 等。当前示例我们添加了 2 个字段 type 是 select 和 text。针对各种 type 我们会添加不同的子节点控制它的显示。
如 select/multiselect 必须定义子节点 source_model
第二步 设置默认值
在 system.xml 刚添加的字段是值是空的 “null”。要想模块在未手动设置参数前正常工作就要指定默认值。默认值是在 etc 文件夹的 config.xml 文件设置。如下代码:
文件: app/code/Aqrun/HelloWorld/etc/config.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="aqrun" translate="label" sortOrder="10">
<label>Aqrun</label>
</tab>
<section id="helloworld" translate="label" sortOrder="130"
showInDefault="1" showInWebsite="1" showInStore="1">
<class>separator-top</class>
<label>欢迎</label>
<tab>aqrun</tab>
<resource>Aqrun_HelloWorld::helloworld_config</resource>
<group id="general" translate="label" type="text" sortOrder="10"
showInDefault="1" showInWebsite="0" showInStore="0">
<label>基础设置</label>
<field id="enable" translate="label" type="select"
showInDefault="1" showInWebsite="0" showInStore="0"
sortOrder="1">
<label>模块启用</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
<field id="display_text" translate="label" type="text"
showInDefault="1" showInWebsite="0" showInStore="0"
sortOrder="1">
<label>输出内容</label>
<comment>指定页面显示的内容</comment>
</field>
</group>
</section>
</system>
</config>
上面代码中默认值放在 <default>
节点,格式是:
<default>
<section>
<group>
<field>{value}</field>
</group>
</section>
</default>
第三步 清空缓存
如下显示:
如果出现 404 错误需要登录重新登录应该可以。
第四步 获取配置参数
首先保存配置并清除缓存,然后就可以从数据库获取保存的值了。
在 system.xml 文件中添加了 2 个字段 enable 和 display_text。获取值的路径就是:
- helloworld/general/enable
- helloworld/general/display_text
4.1 直接获取
$this->scopeConfig->getValue('helloworld/general/enable', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
$this->scopeConfig->getValue('helloworld/general/display_text', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
4.2 添加帮助文件
新建: app/code/Aqrun/HelloWorld/Helper/Data.php
<?php
namespace Mageplaza\HelloWorld\Helper;
use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Store\Model\ScopeInterface;
class Data extends AbstractHelper
{
const XML_PATH_HELLOWORLD = 'helloworld/';
public function getConfigValue($field, $storeId = null)
{
return $this->scopeConfig->getValue(
$field, ScopeInterface::SCOPE_STORE, $storeId
);
}
public function getGeneralConfig($code, $storeId = null)
{
return $this->getConfigValue(self::XML_PATH_HELLOWORLD .'general/'. $code, $storeId);
}
}
接下来在控制器中获取值
文件: app/code/Aqrun/HelloWorld/Controller/Index/Config.php
namespace Aqrun\HelloWorld\Controller\Index;
class Config extends \Magento\Framework\App\Action\Action
{
protected $helperData;
public function __construct(
\Magento\Framework\App\Action\Context $context,
\Aqrun\Helloworld\Helper\Data $helperData
){
$this->helperData = $helperData;
return parent::__construct($context);
}
public function execute()
{
echo $this->helperData->getGeneralConfig('enable');
echo '<br/>';
echo $tihs->helperData->getGeneralConfig('display_text');
exit();
}
}
运行 php bin/magento cache:clean
清除缓存检测结果