We use cookies to make your experience better. To comply with the new e-Privacy directive, we need to ask for your consent to set the cookies. Learn more.
How To Create Admin Menu In Magento 2
Creating an admin menu in Magento 2 involves defining the menu items in the Step 1: Create
Step 2: Create ACL (
Create a
menu.xml
file of your module. This file tells Magento where the menu items should appear in the Admin panel and what actions they should trigger.
Here's a step-by-step guide on how to create an admin menu in Magento 2:
Step 1: Create menu.xml
Create a menu.xml
file in app/code/Vendor/Module/etc/adminhtml
:
<?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> <!-- Main Menu Item --> <add id="Vendor_Module::main_menu" title="Magekwik Settings" module="Vendor_Module" sortOrder="10" resource="Vendor_Module::main_menu" /> <!-- Submenu Item --> <add id="Vendor_Module::general_settings" title="General Settings" module="Vendor_Module" sortOrder="20" parent="Vendor_Module::main_menu" resource="Vendor_Module::general_settings"/> <!-- Another Submenu Item --> <add id="Vendor_Module::advanced_settings" title="Advanced Settings" module="Vendor_Module" sortOrder="30" parent="Vendor_Module::main_menu" action="route/index/controller" resource="Vendor_Module::advanced_settings"/> </menu> </config>
Explanation:
- Main Menu Item:
<add>
: Theadd
tag is used to define a menu item.id
: A unique identifier for the menu item. It should follow the conventionVendor_Module::menu_id
.title
: The label of the menu item as it appears in the Admin panel.module
: The module that this menu belongs to (e.g.,Vendor_Module
).sortOrder
: The order in which the menu item appears relative to others.resource
: The ACL resource associated with this menu item. It defines who can see this menu item based on permissions.action
: The URL path this menu item will direct to. The path is usually in the formatcontroller_name/action_name
(e.g.,adminhtml/magekwik/index
).
- Submenu Items:
parent
: Indicates that this item is a child of another menu item. Theparent
should be set to theid
of the main menu item.action
: The specific controller and action that will handle the request when the menu item is clicked.
Step 2: Create ACL (acl.xml
)
Create a acl.xml
file in app/code/Vendor/Module/etc
:
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Acl/etc/acl.xsd"> <acl> <resources> <resource id="Magento_Backend::admin"> <resource id="Vendor_Module::main_menu" title="Magekwik Settings" sortOrder="10"> <resource id="Vendor_Module::general_settings" title="General Settings" sortOrder="10"/> <resource id="Vendor_Module::advanced_settings" title="Advanced Settings" sortOrder="20"/> </resource> </resource> </resources> </acl> </config>
Explanation:
<resource>
: Eachresource
tag defines a permission that can be associated with a menu item.id
: The ID corresponds to theresource
attribute in themenu.xml
file.title
: The label for the ACL resource.sortOrder
: Determines the order of the resources in the ACL tree.
Summary
This setup creates a basic admin menu in Magento 2, complete with submenus and associated ACL resources to control access. This structure can be extended further based on the needs of your module.