magento----系统配置

    技术2024-07-25  17

    明天就过年了,哈哈!

     

    1

     

    如何获得配置数据

    Mage::getStoreConfig('段',Store_id),譬如:

    Mage::getStoreConfig('helloworld_options/message/hello_message',1);

    可以得到sysytem.xml文件中配置后,然后再后台system-config里面设置的值。

    2

    如何配置数据。

    2.1

    首先在config.xml中要指明helpers,譬如:

    <global>

    <helpers>

    <helloworld>

    <class>Zh_Helloworld_Helper</class>

    </hellowordl>

    </helpers>

    </global>

    2.1.1

    在配置中必须配置helpers类。

    使用helper

    ----------------------->

    Mage::helper('helloworld/foo');

    对用的helpers类Zh_Helloworld_Helper_Foo

    如果是Zh_Helloworld_Helper_Data可以直接通过Mage::helper('helloworld')得到data帮助类;

     

     

    2.1.2帮助类实例:

    app/code/local/Zh/Helloworld/Helper/Data.php

    class Zh_Helloworld_Helper_Data extends Mage_Core_Helper_Abstract

    {

    }

     

    3

    添加新的段(section):

    在etc/system.xml文件中:

    <config>

    <tabs>

    <helloconfig translate="label" module="helloworld">

    <label>Hello Config</label>

    <sort_order>222</sort_order>

    </helloconfig>

    </tabs>

     

    <sections>

    <helloworld_options translate="label" module="helloworld">

    <label>Hello World</labe>

    <tab>helloconfig</tab>

    <frontend_type>text</frontend_type>

    <sort_order>1000</sort_order>

    <show_in_default>1</show_in_default>

    <show_in_website>1</show_in_website>

    <show_in_store>1</show_in_store>

    </helloworld_options>

    </sections>

     

    </config>

    4

    添加acl权限:

    etc/config.xml

    <config>

    <adminhtml>

    <acl>

    <resources>

    <admin>

    <children>

    <system>

    <children>

    <config>

    <children>

    <helloworld_options>

    <title>Store Hello World Module Section</title>

    </helloworld_options>

    </children>

    </config>

    </children>

    </system>

    </children>

    </admin>

    </resources>

    </acl>

    </adminhtml>

    </config>

     

    分析这个代码,所有的代码定义在如下:

    <adminhtml>

    <acl>

    <resources>

    </resources>

    </acl>

    </adminhtml>

     

    5

    添加组:

    在magento中,选项是通过组来划分的,所以在添加选项之前得先添加组,修改system.xml文件

     

    <sections> <helloworld_options translate="label" module="helloworld"> <label>Hello World Config Options</label> <tab>helloconfig</tab> <frontend_type>text</frontend_type> <sort_order>1000</sort_order> <show_in_default>1</show_in_default> <show_in_website>1</show_in_website> <show_in_store>1</show_in_store> <groups> <messages translate="label"> <label>Demo Of Config Fields</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> </messages> </groups> </helloworld_options> </sections>

     

    6

    添加配置选项:

     

    <messages translate="label"> <label>Demo Of Config Fields</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> <hello_message> <label>Message</label> <frontend_type>select</frontend_type> < !– adding a source model –> <source_model>helloworld/words</source_model> <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> </hello_message> </fields> </messages> 6.1 app/code/local/Zhlmmc/Helloworld/Model/Words.php class Zhlmmc_Helloworld_Model_Words { public function toOptionArray() { return array( array('value'=>1, 'label'=>Mage::helper('helloworld')->__('Hello')), array('value'=>2, 'label'=>Mage::helper('helloworld')->__('Goodbye')), array('value'=>3, 'label'=>Mage::helper('helloworld')->__('Yes')), array('value'=>4, 'label'=>Mage::helper('helloworld')->__('No')), ); } }

     

     源模型提供了一个方法“toOptionsArray”,返回的数据时用来填充我们之前定义的配置选项的。这个方法在运行时会被“initFields”调用。“initFields”在以下类中定义

    app/code/core/Mage/Adminhtml/Block/System/Config/Form.php 7

    如何获得配置数据

    到目前为止,我们只是讲了如何设置Magento,可以让用户可以配置我们的模块。现在让我们来看看如何获取用户的配置数据。

    Mage::getStoreConfig('helloworld_options/messages/hello_message');

    上面这行代码就可以获取我们上面配置的那个“select”选项的数据。这个函数的参数是我们要获取的数据的URI,格式如下

    section_name/group_name/field_name

    你也可以通过以下代码来获取一个组或者段的所有值

    Mage::getStoreConfig('helloworld_options/messages'); Mage::getStoreConfig('helloworld_options');

    最后,如果你想获取针对某个特定店面(store)的数据,你可以传入store ID

    Mage::getStoreConfig('helloworld_options',1);

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

    最新回复(0)