Creating an admin menu in Magento 2 involves defining the menu items in the 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>: The add tag is used to define a menu item.
    • id: A unique identifier for the menu item. It should follow the convention Vendor_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 format controller_name/action_name (e.g., adminhtml/magekwik/index).
  • Submenu Items:
    • parent: Indicates that this item is a child of another menu item. The parent should be set to the id 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>: Each resource tag defines a permission that can be associated with a menu item.
  • id: The ID corresponds to the resource attribute in the menu.xml file.
  • title: The label for the ACL resource.
  • sortOrder: Determines the order of the resources in the ACL tree.
Then, log in to the Magento Admin panel, and you should see your new menu items under the specified section.

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.