Magento: helloworld report page in the Admin report menu

To add a new page in Magento admin panel, at least 6 files are needed in a Magento module. 2 xml files, 1 template file, 1 block class, 1 admin controller class, and 1 helper class. Here is an example of adding a new menu item in the report drop down menu in Magento admin panel.

app/code/local/MyExtensions/Helloworld/etc/config.xml, blocks, hellpers, and admin router need to be defined.

<config>
<!-- some other configurations, module version, frontend... -->
    <global>
        <blocks>
            <helloworld>
                <class>MyExtensions_Helloworld_Block</class>
            </helloworld>
        </blocks>
        <helpers>
            <helloworld>
                <class>MyExtensions_Helloworld_Helper</class>
            </helloworld>
        </helpers>
    </global>
    <admin>
        <routers>
            <adminhtml>
                <args>
                    <modules>
                        <helloworld before="Mage_Adminhtml">MyExtensions_Helloworld_Adminhtml</helloworld>
                    </modules>
                </args>
            </adminhtml>
        </routers>
    </admin>
    <!-- some other configurations -->
</config>

app/code/local/MyExtensions/Helloworld/etc/adminhtml.xml, defines the helloworld menu item and it’s action to be app/code/local/MyExtensions/Helloworld/controllers/Adminhtml/HelloworldController.php

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <menu>
        <report>
            <children>
                <helloworld_report translate="title" module="helloworld">
                    <title>Helloworld Report</title>
                    <action>adminhtml/helloworld/index</action>
                    <sort_order>90</sort_order>
                </helloworld_report>
            </children>
         </report>
    </menu>
</config>

app/design/adminhtml/template/helloworld/adminhelloworld.phtml, the block template that renders the html contents.

<?php
// $this @ MyExtensions_Helloworld_Block_Adminhtml_Adminhelloworld
var_dump(get_class_methods(get_class($this)));
$this->helloAdmin();
?>

app/code/local/MyExtensions/Helloworld/Helper/Data.php, module helper class.

<?php
class MyExtensions_Helloworld_Helper_Data extends Mage_Core_Helper_Abstract {
    
}
?>

app/code/local/MyExtensions/Helloworld/Block/Adminhtml/Adminhelloworld.php, the admin helloworld block class for the block adminhelloworld.phtml

<?php
class MyExtensions_Helloworld_Block_Adminhtml_Adminhelloworld extends Mage_Adminhtml_Block_Template
{

    public function helloAdmin()
    {
        echo "Hello Admin";
    }
}
?>

app/code/local/MyExtensions/Helloworld/controllers/Adminhtml/HelloworldController.php, the controller for the admin helloworld page

<?php
class MyExtensions_Helloworld_Adminhtml_HelloworldController extends Mage_Adminhtml_Controller_Action
{
    public function indexAction()
    {   
        // Load layout.
        $this->loadLayout();
        
        // Set block and template to use for admin.
        $this->_addContent( $this->getLayout()
        ->createBlock( 'helloworld/adminhtml_adminhelloworld' )
        ->setTemplate( 'helloworld/adminhelloworld.phtml' ) );

        // Render layout.
        $this->renderLayout();
    }    
}
?>

Search within Codexpedia

Custom Search

Search the entire web

Custom Search