Create new Admin Controller in Magento

create new admin controller

Step 1

Create a new module with the following entries in config.xml

<config>
    <modules>
        <Scommerce_HelloWorld>
            <version>0.0.1</version>
        </Scommerce_HelloWorld>
    </modules>

    <admin>
        <routers>
            <helloworld>
                <use>admin</use>
                <args>
                    <module>Scommerce_HelloWorld</module>
                    <frontName>helloworld</frontName>
                </args>
            </helloworld>
        </routers>
    </admin>
</config>

Step 2

Create controller Class under \app\code\local\Scommerce\HelloWorld\controllers\IndexController.php

<?php
class Scommerce_HelloWorld_IndexController 
            extends Mage_Adminhtml_Controller_Action
{
    public function indexAction()
    {
        $this->loadLayout();

        $block = $this->getLayout()
                        ->createBlock('core/text', 'hello-world-block')
                        ->setText('<strong>Hello World</strong>');

        $this->_addContent($block);

        $this->renderLayout();

    }
}

N.B. – If you were overwriting frontend controller the above should have done the job. But with admin controllers they don’t work directly hitting the URL because of the permission.

Step 3

You can add the following admin menu information in config.xml to access your new page -:

<adminhtml>
        <menu>
            <admin_first_menu translate="title">
                <title>Admin First Menu</title>
                <sort_order>100</sort_order>
                <children>
                    <admin_first_page translate="title">
                    <title>Admin First Page</title>
                    <action>helloworld/index</action>
                    </admin_first_page>
                </children>
            </admin_first_menu>
        </menu>
</adminhtml>

Step 4

This is the most important step of all!

Create your module file in \etc\modules\Scommerce_HelloWorld.xml

<config>
    <modules>
        <Scommerce_HelloWorld>
                <active>true</active>
                <codePool>local</codePool>
            </Scommerce_HelloWorld>
    </modules>
<config>

N.B – You can add your new menu as part of permissions in config.xml or adminhtml.xml. Check our permission article for more information.

4 thoughts on “Create new Admin Controller in Magento

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.