在后台配置一些常量,比硬编码的方式更加灵活,不需要重新改写代码并上线才能生效,直接修改后台的配置就可以了
后台配置文件
在模块的etc文件夹下新建system.xml和adminhtml.xml
编辑如下:
<?xml version="1.0"?>
<config>
<tabs>
<appconfig translate="label" module="nano_app"> <!--nano_app是模块名-->
<label>App Options</label>
<sort_order>9999999</sort_order>
</appconfig>
</tabs>
<sections>
<app_options translate="label" module="nano_app"> <!--app_options是配置的唯一标识-->
<label>Settings</label>
<tab>appconfig</tab>
<frontend_type>text</frontend_type>
<sort_order>1000</sort_order> <!--sort_order是配置的顺序-->
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
<groups>
<app_setting translate="label">
<label>app的后台配置</label>
<frontend_type>text</frontend_type>
<sort_order>1</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
<fields>
<user_name> <!--配置的用户名的唯一标识-->
<label>UserName</label>
<frontend_type>text</frontend_type>
<comment>用户名</comment>
<sort_order>1</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
</user_name>
<pass_word>
<!--配置的密码的唯一标识-->
<label>Password</label>
<frontend_type>text</frontend_type>
<comment>密码</comment>
<sort_order>2</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
</pass_word>
</fields>
</app_setting>
</groups>
</app_options>
</sections>
</config>
<?xml version="1.0"?>
<config>
<acl>
<resources>
<admin>
<children>
<system>
<children>
<config>
<children>
<app_options>
<title>App Options</title>
</app_options>
</children>
</config>
</children>
</system>
</children>
</admin>
</resources>
</acl>
</config>
Helper类
这时候若访问magento的后台,会报下面的错误:
说明我们需要建立App这个模块的helper类
先修改config.xml配置
<global>
<helpers>
<nano_app>
<class>Nano_App_Helper</class>
</nano_app>
</helpers>
</global>
再在App模块下建立Helper文件夹,新建Data.php
注:
Data.php是默认访问的Helper类
<?php
class Nano_App_Helper_Data extends Mage_Core_Helper_Abstract
{
}
配置成功后就能在后台对App模块配置一些参数了
配置的路径为:System > Configuration > APP OPTIONS > Settings

使用后台配置
配置完成后,如果要使用这个配置,可使用Mage::getStoreConfig()方法
$userName = Mage::getStoreConfig("app_options/app_setting/user_name");
$passWord = Mage::getStoreConfig("app_options/app_setting/pass_word");
补充:M2 的系统配置用 system.xml
原文讲的是给模块加系统配置项。M2 的做法:
<!-- etc/adminhtml/system.xml -->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
<system>
<tab id="vendor" translate="label" sortOrder="100">
<label>Vendor</label>
</tab>
<section id="vendor" translate="label" type="text" sortOrder="10"
showInDefault="1" showInWebsite="1" showInStore="1">
<label>Vendor Config</label>
<tab>vendor</tab>
<group id="general" translate="label" type="text" sortOrder="10">
<label>General</label>
<field id="api_key" translate="label" type="text"
sortOrder="10" showInDefault="1">
<label>API Key</label>
<backend_model>Magento\Config\Model\Config\Backend\Encrypted</backend_model>
</field>
</group>
</section>
</system>
</config>
读配置的方式:
// 不要用 getStoreConfig(已废弃)
$this->scopeConfig->getValue('vendor/general/api_key', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
三个关键点:
showInDefault/showInWebsite/showInStore三个都为 0 时,这个字段在任何层级都看不到——这是"配置项加了却看不见"的头号原因。- 敏感值(密钥、密码)务必加
backend_model做加密存储。 - 改完需要
bin/magento cache:flush。
本文整理于 2018 年,2026 年 11 月补写。