Adding a New Admin Menu Item

If you create a new extension for Magento, chances are that you will need something in the Admin Panel to manage your data. Sometimes, this can be managed from the System > Configuration section, but other times, and new section is needed. In this short post we will give an example of what is needed to create a new menu item in the top navigation in the Admin Panel.

The module that we are working in is the Super_Awesome module. In our local code pool we have a directory structure like:

Super
  |_ Awesome
        |_etc
        |  |_ adminhtml.xml
        |  |_ config.xml
        |_Helper
            |_ Data.php

If you didn't already know, all of the .xml files in the etc directory get jammed together into one big Config object (notice both files begin with the <config> element), so you can accomplish the same thing in one .xml file or many. The standard is to create many to better separate what is being configured.

adminhtml.xml contents:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?xmlversion="1.0"?>
<config>
    <menu>
        <awesometranslate="title"module="awesome">
            <title>Awesome</title>
            <sort_order>15</sort_order>
            <children>
                <exampletranslate="title"module="awesome">
                    <title>Example</title>
                    <sort_order>1</sort_order>
                    <action>adminhtml/example/index</action>
                </example>
            </children>
        </awesome>
    </menu>
</config>

This configuration will create an upper level menu item called "Awesome" and when you hover over it, it will show the child menu item called "Awesome". The <action> tag indicates that if you click on that menu item, it will make a request to the given url. Although the "Example" menu item has an action, if you click on it, it will fail because there is no controller in this example  .

config.xml contents:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?xmlversion="1.0"?>
<config>
    <modules>
        <Super_Awesome>
            <version>0.1.0</version>
        </Super_Awesome>
    </modules>
    <adminhtml>
        <!-- The <layout> updates allow us to define our block layouts in a separate file so are aren't messin' with the Magento layout files.  -->
        <layout>
            <updates>
                <awesome>
                    <file>awesome.xml</file>
                </awesome>
            </updates>
        </layout>
        <!-- The <acl> section is for access control. Here we define the pieces where access can be controlled within a role. -->
        <acl>
            <resources>
                <admin>
                    <children>
                        <awesome>
                            <title>Awesome Menu Item</title>
                            <children>
                                <exampletranslate="title"module="awesome">
                                    <title>Example Menu Item</title>
                                </example>
                            </children>
                        </awesome>
                    </children>
                </admin>
            </resources>
        </acl>
    </adminhtml>
    <global>
        <helpers>
            <awesome>
                <class>Super_Awesome_Helper</class>
            </awesome>
        </helpers>
    </global>
</config>

Data.php contents:

?
1
2
3
4
5
6
<?php
 
classSuper_Awesome_Helper_DataextendsMage_Core_Helper_Abstract
{
 
}

Why an empty class? Well, in some of the xml elements we have defined: translate="title" module="awesome". This tells whatever is reading the config to use the default Helper in the "awesome" module to do its translations of the titles, etc. If you didn't have this, you would see an error like:

Fatal error: Class 'Super_Awesome_Helper_Data' not found in .../magento-1.5.0.1/app/Mage.php on line 523.

That's it! Clear Cache, Logout, Login and your menu item should appear at the top!

转载于:https://my.oschina.net/stream/blog/103190

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值