magento自定义后台配置

本文详细介绍如何在Magento中创建自定义模块及其配置流程。包括模块配置文件的建立、系统配置界面的定制以及如何从前端获取这些配置信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

示例路径如下:D:\htdocs\magento_a\app\code\local\Exercise\Avatar

第一步:建module的配置文件 D:\htdocs\magento_a\app\etc\modules\Exercise_Avatar.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Exercise_Avatar>
            <active>true</active>
            <codePool>local</codePool>
        </Exercise_Avatar>
    </modules>
</config>
第二步:D:\htdocs\magento_a\app\code\local\Exercise\Avatar\etc\config.xml


<?xml version="1.0"?>
<config>
    <modules>
        <Exercise_Avatar>
            <version>0.1.0</version>
        </Exercise_Avatar>
    </modules>
    <frontend>
        <routers><!--创建前台访问的路由-->
            <avatar>
                <use>standard</use>
                <args>
                    <module>Exercise_Avatar</module><!--注意此处的值应该和当前创建的module一致-->
                    <frontName>avatar</frontName>
                </args>               
            </avatar>
        </routers>  
    </frontend>
    <global>
        <helpers>
            <avatar>
                <class>Exercise_Avatar_Helper</class>
            </avatar>
        </helpers>
    </global>
    
    <adminhtml>
        <acl>
            <resources>
                <admin>
                    <children>
                        <system>
                            <children>
                                <config>
                                    <children>
                                        <exercise><!--注意此处的mode name,与下面的system.xml的config->sections->exercise对应-->
                                            <title>avatar</title><!--此节点的值可随意修改-->
                                        </exercise>
                                    </children>
                                </config>
                            </children>
                        </system>
                    </children>
                </admin>
            </resources>
        </acl>
    </adminhtml>
</config>

第三步:system.xml


<config>
    <tabs>
        <avatar translate="label" module="avatar">
            <label>Avatar</label>
            <sort_order>100</sort_order>
        </avatar>
    </tabs>
    <sections>
        <exercise translate="label" module="avatar"><!-- 这里的节点名和上面config.xml中的节点对应,它们需要保持一致-->
            <label>Set customer avatar profile</label>
            <tab>avatar</tab>
            <frontend_type>text</frontend_type>
            <sort_order>100</sort_order>
            <show_in_default>1</show_in_default>
            <show_in_website>1</show_in_website>
            <show_in_store>1</show_in_store>
            <groups>
                <avatar translate="label">
                    <label>set max size of picture</label>
                    <frontend_type>text</frontend_type>
                    <sort_order>10</sort_order>
                    <show_in_default>1</show_in_default>
                    <show_in_website>1</show_in_website>
                    <show_in_store>1</show_in_store>
                    <fields>
                        <avatar_width translate="label">
                            <label>width</label>
                            <frontend_type>text</frontend_type>
                            <sort_order>10</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>1</show_in_store>
                            <comment>px</comment>
                        </avatar_width>
                        <avatar_height translate="label">
                            <label>height</label>
                            <frontend_type>text</frontend_type>
                            <sort_order>10</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>1</show_in_store>
                            <comment>px</comment>
                        </avatar_height>
                    </fields>
                </avatar>
                <enable translate="label">
                    <label>show avatar</label>
                    <frontend_type>text</frontend_type>
                    <sort_order>10</sort_order>
                    <show_in_default>1</show_in_default>
                    <show_in_website>1</show_in_website>
                    <show_in_store>1</show_in_store>
                    <fields>                       
                        <avatar_enable translate="label">
                            <label>enable avatar</label>
                            <frontend_type>select</frontend_type>
                            <source_model>adminhtml/system_config_source_yesno</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>
                            <comment>show avatar in frontend</comment>
                        </avatar_enable>
                    </fields>
                </enable>
            </groups>
        </exercise>
    </sections>

</config>

第四步: D:\htdocs\magento_a\app\code\local\Exercise\Avatar\Helper\Data.php

<?php
class Exercise_Avatar_Helper_Data extends Mage_Core_Helper_Abstract
{

}

此时可刷新后台,新设定的配置将会显示在system->configuration,general的下面.点击进去可以看到所设置的两组配置项.结果如下图.

第五步:

取配置所设定的值

D:\htdocs\magento_a\app\code\local\Exercise\Avatar\controllers\IndexController.php

<?php

class Exercise_Avatar_IndexController extends Mage_Core_Controller_Front_Action {

    public function indexAction() {
        echo Mage::getStoreConfig('exercise/avatar/avatar_width');//注意此处参数与节点对应
        echo Mage::getStoreConfig('exercise/avatar/avatar_height');
        echo Mage::getStoreConfig('exercise/enable/avatar_enable');
    }

}


注意:如果点''Set customer avatar profile'',右边显示404错误,重新登录后可解决. 如有其它问题,欢迎留言.

相关源代码可从下面链接下载:http://download.youkuaiyun.com/detail/emily_init/4600477

关于xml更详细的解释可见:http://www.magentocommerce.com/wiki/5_-_modules_and_development/admin/xml_structure_for_admin_configurations


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值