magento2 后台config配置和读取配置

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="Maijindou" translate="label" sortOrder="1">
            <label>Maijindou</label>
        </tab>
        <section id="Maijindou_Customer_admin" translate="label" type="text" sortOrder="3" showInDefault="1" showInWebsite="1" showInStore="1">
            <label>会员API配置</label>
            <tab>Maijindou</tab>
            <resource>Maijindou_Customer::configurations</resource>
            <group id="general" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
                <label>基础设置</label>
                <field id="maijindou_url" translate="label" type="text" sortOrder="20" showInDefault="1"
                       showInWebsite="1" showInStore="1">
                    <label>请求地址</label>
                </field>
                <field id="maijindou_app_id" translate="label" type="text" sortOrder="20" showInDefault="1"
                       showInWebsite="1" showInStore="1">
                    <label>APP_ID</label>
                </field>
                <field id="maijindou_sc" translate="label" type="text" sortOrder="20" showInDefault="1"
                       showInWebsite="1" showInStore="1">
                    <label>SC</label>
                </field>
                <field id="page_layout" translate="label comment" type="select" sortOrder="29" showInDefault="1"
                       showInWebsite="1" showInStore="1">
                    <label>账号类型</label>
                    <source_model>Maijindou\Customer\Model\Layout</source_model>
                </field>
            </group>
        </section>
    </system>
</config>

下拉框选项
Maijindou\Customer\Model\Layou



namespace Maijindou\Customer\Model;

class Layout implements \Magento\Framework\Option\ArrayInterface{
    
    public function toOptionArray(){
        return [
            ['value' => '1', 'label' => __('手机')],
            ['value' => '2', 'label' => __('邮箱')],
        ];
    }
    
    public function toArray(){
        return [
            '1' => __('手机'),
            '2'  => __('邮箱'),
        ];
    }
}

最终呈现效果
magento2 后台config配置和读取配置_第1张图片

获取配置信息

    public function __construct(\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig)
    {
        //获取后台config配置
        $this->_scopeConfig = $scopeConfig;
        $this->url = $this->_scopeConfig->getValue('Maijindou_Customer_admin/general/maijindou_url', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
       
    }

你可能感兴趣的:(magento)