Creating Admin Menu for Magento Custom Module: The Complete Guide
As a senior developer working on custom Magento modules, you often need to create admin menus that provide quick access to your module’s configuration settings. In this guide, we’ll walk through the process of adding an “Admin menu” setting to your custom module using XML configuration files.
Understanding the Menu Configuration File
The menu XML file is responsible for defining the structure and hierarchy of the admin menu in Magento. By default, it resides under the app/code/Company/Module/etc/adminhtml/menu.xml folder.
Here’s a basic example of what the menu.xml file looks like:
<menu>
<finder module="finder">
<title>Finder</title>
<sort_order>71</sort_order>
<children>
<items module="finder">
<title>Manage Finder</title>
<sort_order>0</sort_order>
<action>finder/adminhtml_finder</action>
</items>
<items module="finder">
<title>Setting</title>
<sort_order>0</sort_order>
<action></action>
</items>
</children>
</finder>
</menu>
The menu.xml file consists of a single <menu> root element that contains one or more <finder> elements. Each <finder> element represents a top-level menu item and includes the following attributes:
module: The module name for which this menu is configured.title: The display label for the menu item.sort_order: Determines the position of the menu item within the admin menu hierarchy.
Defining the Action Attribute
The critical piece of information missing in your provided XML snippet is the <action> attribute under the “Setting” items tag. This attribute specifies the URL to which the user will be redirected when they click on this menu item.
To configure the action for your custom module, you need to follow these steps:
- Create a Controller: First, ensure that you have created an admin controller for your module. For example, you might create
app/code/Company/Module/controllers/Adminhtml/SettingController.php.
class SettingController extends \Magento\Backend\App\Action
{
public function execute()
{
$this->_forward('edit');
}
}
- Configure the Controller in your Module’s routes.xml: In the
app/code/Company/Module/etc/adminhtml/routes.xmlfile, add or update the route definition for your custom controller:
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Adminhtml:etc/adminhtml.xsd">
<menu>
<items parent="finder::finder">
<item name="setting" sortOrder="5">
<title translate="true">Setting</title>
<action>finder/something/setting</action>
</item>
</items>
</menu>
<route id="finder" frontName="something">
<area>adminhtml</area>
</route>
<virtualTypes>
<type name="Magento\Backend\App\Action\Context">
<arguments>
<argument name="request" xsi:type="object">Finder\Controller\Adminhtml\Setting</argument>
</arguments>
</type>
</virtualTypes>
</config>
In the above example, replace finder with your actual module name and something with a unique identifier for your frontName.
- Update the menu.xml File: Now you can update the
<action>attribute in yourmenu.xmlfile to point to the new route:
<items module="finder">
<title>Setting</title>
<sort_order>0</sort_order>
<action>finder/something/setting</action>
</items>
Conclusion
Creating an admin menu for a custom Magento module involves a combination of XML configuration files and controller setup. By following the steps outlined above, you can create a user-friendly admin interface that allows your store administrators to easily navigate to your module’s configuration settings.
Remember, keeping your code organized and adhering to Magento’s naming conventions will save you time in the long run. For more in-depth guides on Magento development, visit our blog at Laravel Company.