Green Acorn

Blog

Building a Magento Admin Module

Have you ever wanted to build a custom module for the Magento admin panel? I know I did and it took me a while to get all the information together in order to build one. Today I am going to share this information hoping you as a web developer will find my research useful.

Creating the Module Folder Structure

Lets start by creating the following folders:

app/code/local/Greenacorn/Greenmodule/etc
app/code/local/Greenacorn/Greenmodule/controllers
app/code/local/Greenacorn/Greenmodule/Helper

Building the Module

Within the ‘etc’ folder create a file called config.xml and paste in the following code:

<?xml version="1.0"?>
<config>
	<modules>
		<greenacorn_greenmodule>
			<version>1.0.0</version>
			<depends>
				<!-- no dependencies -->
			</depends>
		</greenacorn_greenmodule>
	</modules>
	<global>
		<models />
		<blocks />
		<resources />
		<extraconfig />
		<helpers>
			<greenmodule>
				<class>Greenacorn_Greenmodule_Helper</class>
			</greenmodule>
		</helpers>
	</global>
	<admin>
		<routers>
			<greenacorn_greenmodule>
				<use>admin</use>
				<args>
					<module>Greenacorn_Greenmodule</module>
					<frontName>greenmodule</frontName>
				</args>
			</greenacorn_greenmodule>
		</routers>
	</admin>
</config>

Next create a file called adminhtml.xml within the ‘etc’ folder and paste in the following:

<?xml version="1.0" ?>
<config>
	<menu>
		<green_menu translate="title" module="greenmodule">
			<title>Green Acorn</title>
			<sort_order>9999</sort_order>
			<action>greenmodule/index/index</action>
		</green_menu>
	</menu>
</config>

We need to tell Magento to load our module and to do this we need to create a file within /app/etc/modules. I called my file Green_All.xml and it contained the below XML code:

<?xml version="1.0"?>
<config>
    <modules>
        <Greenacorn_Greenmodule>
            <active>true</active>
            <codePool>local</codePool>
        </Greenacorn_Greenmodule>
    </modules>
</config>

Now if you refresh your Magento admin panel, you should notice a new tab called ‘Green Acorn’. However, if you click on it you will be given a PHP error saying that Magento can’t find a helper class. This is because within adminhtml.xml we told Magento that the tab is associated with the module ‘greenacorn’ and by default Magento looks for a helper file called Data.php.

module="greenmodule"

In the config file we also declared the helper class within the global node.

	<global>
		<!-- ... -->
		<helpers>
			<greenmodule>
				<class>Greenacorn_Greenmodule_Helper</class>
			</greenmodule>
		</helpers>
	</global>

To fix this problem we need to create a file called Data.php with the Helper folder (/app/ code/ local/ Greenacorn/ Greenmodule/ Helper/ Data.php). This will be an empty class for the time being but it will fix the warning we are currently getting. Paste the following into Data.php:

<?php
class Greenacorn_Greenmodule_Helper_Data extends Mage_Core_Helper_Abstract
{}

Note: The class name needs to be file path structure to Helper/Data.php.

Adding the Controller

The final stage is to add the controller. This will route the user to the method specified within adminhtml.xml. For example lets take the following:

<action>greenmodule/index/index</action>

Magento will look in the module ‘Greenmodule’ for an index controller with the method called indexAction. So lets create a php file called ‘IndexController.php’ within the controllers folder (/app/ code/ local/ Greenacorn/ Greenmodule/ controllers/ IndexController.php). Paste in the below PHP code:

<?php
class Greenacorn_Greenmodule_IndexController extends Mage_Adminhtml_Controller_Action
{  
    public function indexAction()
    {
    	$this->loadLayout();
    	
    	$block = $this->getLayout()->createBlock('core/text', 'green-block')->setText('<h1>Green Acorn Web Design</h1>');
        $this->_addContent($block);
        
    	$this->_setActiveMenu('green_menu')->renderLayout();      
    }   
}

The above method simply creates a block with a the heading ‘Green Acorn Web Design‘ by using Magento factory methods. Refresh your browser and you click on the custom menu tab – you should now see a brand new page with the h1 tag.

Note: You may have to clear your magento cache or log out and log back in for the new page to show. This has caught me out many times.

I hope this magento web development tutorial has been useful and in the next Magento tutorial I will be extending this module by using Models and template / layout files.

Signing out.
Green Acorn

Author:
Posted in: Magento
3 comments on “Building a Magento Admin Module
  1. Pingback: Creation module magento | My Coding Blog

Leave a Reply

Your email address will not be published. Required fields are marked *

     

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>