Magento How to create a Backend Controller


This Controller will add a new tab in Magento 1 Admin with custom option. In this example, we will do the basics, and we will create a simple new tab called MageCheck with two options. We will do the same for Magento 2 and create a Backend Controller for Magento 2 Admin. Let's Follow the steps below:


Step 1: Declare Controller


Magento 1


File path: app/etc/modules/MageCheck_CreateAdminController.xml

<?xml version='1.0'?> <config> <modules> <MageCheck_CreateAdminController> <active>true</active> <codePool>local</codePool> </MageCheck_CreateAdminController> </modules> </config>

Magento 2


File path: app/code/MageCheck/Tutorial/etc/adminhtml/routes.xml

<?xml version='1.0'?> <config xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:noNamespaceSchemaLocation='urn:magento:framework:App/etc/routes.xsd'> <router id='admin'> <route id='tutorial' frontName='tutorial'> <module name='MageCheck_Tutorial' before='Magento_Backend' /> </route> </router> </config>

Step 2: Configure and Register Controller


Magento 1


File path: app/code/local/MageCheck/CreateAdminController/etc/config.xml

<?xml version='1.0'?> <config> <modules> <MageCheck_CreateAdminController> <version>1.0.0</version> </MageCheck_CreateAdminController> </modules> <global> <helpers> <magecheck_createadmincontroller> <class>Mage_Core_Helper</class> </magecheck_createadmincontroller> </helpers> </global> <admin> <routers> <adminhtml> <args> <modules> <foo_bar before='Mage_Adminhtml'>MageCheck_CreateAdminController_Adminhtml</foo_bar> </modules> </args> </adminhtml> </routers> </admin> </config>

Magento 2


File path: app/code/MageCheck/Tutorial/registration.php

<?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'MageCheck_Tutorial', __DIR__ );


Step 3: Create Controller Function


Magento 1


File path: app/code/local/MageCheck/CreateAdminController/controllers/

Adminhtml/CustomController.php

<?php class MageCheck_CreateAdminController_Adminhtml_CustomController extends Mage_Adminhtml_Controller_Action { public function indexAction() { $this->loadLayout() ->_setActiveMenu('mycustomtab') ->_title($this->__('Index Action')); // ... $this->renderLayout(); } public function listAction() { $this->loadLayout() ->_setActiveMenu('mycustomtab') ->_title($this->__('List Action')); // ... $this->renderLayout(); } }

Magento 2


File path: app/code/MageCheck/Tutorial/Controller/Adminhtml/Index/Index.php

<?php namespace MageCheck\Tutorial\Controller\Adminhtml\Index; class Index extends \Magento\Backend\App\Action { protected $resultPageFactory; public function __construct( \Magento\Backend\App\Action\Context $context, \Magento\Framework\Registry $coreRegistry, \Magento\Framework\View\Result\PageFactory $resultPageFactory ) { $this->resultPageFactory = $resultPageFactory; parent::__construct($context, $coreRegistry); } public function execute() { echo 'My admin controller'; exit; } protected function _isAllowed() { return true; } }

Step 4: Create Menu Options


Magento 1


File path: app/code/local/MageCheck/CreateAdminController/etc/adminhtml.xml

<?xml version='1.0' encoding='UTF-8'?> <config> <menu> <mycustomtab module='magecheck_createadmincontroller' translate='title'> <title>MageCheck</title> <sort_order>100</sort_order> <children> <index module='magecheck_createadmincontroller' translate='title'> <title>Tutorial 1</title> <sort_order>1</sort_order> <action>adminhtml/custom</action> </index> <list module='magecheck_createadmincontroller' translate='title'> <title>Tutorial 2</title> <sort_order>2</sort_order> <action>adminhtml/custom/list</action> </list> </children> </mycustomtab> </menu> <acl> <resources> <admin> <children> <custom translate='title' module='magecheck_createadmincontroller'> <title>My Controller</title> <sort_order>-100</sort_order> <children> <index translate='title'> <title>Index Action</title> <sort_order>1</sort_order> </index> <list translate='title'> <title>List Action</title> <sort_order>2</sort_order> </list> </children> </custom> </children> </admin> </resources> </acl> </config>

Magento 2


File path: app/code/MageCheck/Tutorial/etc/adminhtml/menu.xml

<?xml version='1.0'?> <config xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:noNamespaceSchemaLocation='urn:magento:module:Magento_Backend:etc/menu.xsd'> <menu> <add id='MageCheck_Tutorial::helloworld' title='MageCheck' module='MageCheck_Tutorial' sortOrder='51' resource='MageCheck_Tutorial::helloworld'/> <add id='MageCheck_Tutorial::post' title='Tutorials' module='MageCheck_Tutorial' sortOrder='10' action='magecheck_tutorial/post' resource='MageCheck_Tutorial::post' parent='MageCheck_Tutorial::helloworld'/> <add id='MageCheck_Tutorial::hello_configuration' title='Configuration' module='MageCheck_Tutorial' sortOrder='99' parent='MageCheck_Tutorial::helloworld' action='adminhtml/system_config/edit/section/helloworld' resource='MageCheck_Tutorial::helloworld_configuration'/> </menu> </config>

Step 5: Flush Magento Cache


Log in to Magento Admin panel, Flush Magento cache and see the result. This is a basic way to create admin controller, you can customize the options, add more functions and so on.


Your Backend Controller is created!