Magento 2 : Admin Menu and Route

Before Creating etc files visit  Admin Module Structure
menu.xml
The admin menu in magento2 has 3 parts
The main admin menu title which shows up in the left sidebar
Submenu title
Actual menu
<?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="NameSpace_ModuleName::mainmenu" title="Main Menu" translate="title" module="NameSpace_ModuleName" sortOrder="40" resource="NameSpace_ModuleName::mainmenu"/>
        
        <add id="NameSpace_ModuleName::submenu" title="SubMenu" translate="title" module="NameSpace_ModuleName" sortOrder="10" parent="NameSpace_ModuleName::mainmenu" resource="NameSpace_ModuleName::submenu"/> 

        <add id="NameSpace_ModuleName::actualmenu" title="Actual menu" translate="title" module="NameSpace_ModuleName" sortOrder="20" parent="NameSpace_ModuleName::submenu" action="mainmenu/submenu" resource="NameSpace_ModuleName::actualmenu"/>
    
</menu>
</config>
As it can be seen from the menu.xml structure, the new menu item is typically added using an add directive with it’s parameters
id – unique node identifier, should follow the format: Vendor_Module::menu_item_description
title – text that is shown in a menu
module – current module
sortOrder – where to place menu item
resource – defines the ACL rule which the admin user must have in order to see and access this menu, it is defined in acl.xml. Otherwise, menu item won’t be rendered. For simplicity, Magento_Backend:: content is used.
action – defined above, tells Magento to generate a link to certain admin controller.
parent – defines on which first level a menu item depends.

routes.xml :
router.xml is responsible for url. routeId is used in url and it execute controller action
<config>
    <router id="routerId">
        <route id="routeId" frontName="frontName">
            <module name="NameSpace_Moduluename" />
        </route>
    </router>
</config>
frontName - specifies the first segment after the base URL of a request.
routeId - specifies the unique node id for this route in Magento.

 module.xml : 
A module declares itself (that is, defines its name and existence) in the module.xml file.
<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
 <module name="NameSpace_modulename" setup_version="1.0.0"/>
</config>
acl.xml
acl.xml is responsible for access permission in admin module.
<?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="NameSpace_Module::MenuParentResourceId" sortOrder="15" title="ParentName">
                        <resource id="NameSpace_Module::controllerAction" sortOrder="10" title="Name">
                        </resource>    
   
                    </resource>    
                </resource>
            </resources>
        </acl>
</config>
Our resource will be placed as a child of Magento_Backend::admin. After refreshing the cache visit Role Resource Page in Admin your ParentName is visible. Now you can assign your module in any users group.

Comments