Creating a New Admin Page in Magento: A Step-by-Step Guide
Hello everyone! Today we’re going to tackle a common question from our Magento community: how to create a new admin page within your custom extension. We’ll be using an example provided by a user who is trying to add a simple “About” tab to their “SMS Center” in the admin panel.
Understanding the Current Setup
The user has already created some core files and configuration:
-
config.xml: In this file, the user has registered their module and added event observers for order saving events. They’ve also declared helpers and set up routing for the admin area. -
adminhtml.xml: This file contains menu and ACL configurations to create a new “SMS Center” tab in the admin panel with three child tabs: Send SMS, Settings, and About.
The Current Issue: 404 Error on Frontend
When clicking the “About” tab, the user encounters an awkward 404 error on the frontend instead of being redirected to a custom admin page. This happens because the routing configuration for this tab is set incorrectly in adminhtml.xml:
<about translate="title" module="smsnotification">
<title>About</title>
<action>adminhtml/smsnotification_about</action>
<sort_order>11</sort_order>
</about>
The issue lies in the <action> tag. The user has used a standard Magento admin URL format (adminhtml/{controller}/{action}), but they haven’t actually created these controller and action files.
Creating the Custom Admin Page
To fix this, we need to create a new admin controller and corresponding view file for our “About” page. Let’s go through the steps:
Step 1: Create the Controller
Create a new folder structure app/code/community/VivasIndustries/SmsNotification/controllers/Adminhtml/. Inside this folder, create a new PHP file named AboutController.php.
In AboutController.php, define your controller class and override the indexAction method:
<?php
class VivasIndustries_SmsNotification_Adminhtml_AboutController extends Mage_Adminhtml_Controller_Action
{
public function indexAction()
{
$this->loadLayout()->renderLayout();
}
}
Step 2: Create the View File
Create a new folder structure app/design/adminhtml/default/default/layout/. Inside this layout folder, create a new XML file named smsnotification.xml.
In smsnotification.xml, define your custom layout for the “About” page:
<?xml version="1.0"?>
<layout version="1.0">
<default>
<reference name="content">
<block type="core/text_html" name="about_page">
<action method="setText"><text>This is a custom About Page for the SMS Center extension.</text></action>
</block>
</reference>
</default>
</layout>
Step 3: Update Your XML Files
Finally, update your config.xml and adminhtml.xml files:
In config.xml, add the following routing configuration to map “adminhtml/about” to your new controller:
<admin>
<routers>
<adminhtml>
<args>
<modules>
<VivasIndustries_SmsNotification before="Mage_Adminhtml">VivasIndustries_SmsNotification_Adminhtml</VivasIndustries_SmsNotification>
</modules>
</args>
</adminhtml>
</routers>
</admin>
In adminhtml.xml, update the “About” tab configuration:
<about translate="title" module="smsnotification">
<title>About</title>
<action>adminhtml/about_index</action>
<sort_order>11</sort_order>
</about>
Conclusion
That’s it! With these steps, you should now have a working “About” tab in your “SMS Center” with a simple custom text message. The key takeaways are:
- Always create the corresponding controller and view files for any admin page you want to display.
- Use the correct routing configuration (
adminhtml/{controller}_{action}) in your XML files. - Make sure to include your extension’s namespace in all related files.
I hope this guide has clarified the process of creating custom admin pages in Magento. If you found this helpful, feel free to explore our other tutorials on Laravel Company. Happy coding!