Magento 2 Create Custom Admin Menu Items

Magento 2 Create Custom Admin Menu Items

One main advantage of Magento 2 over Magento 1 is the classic and beautiful admin panel interface. Developers who are into creating extensions for Magento 2 would invariably need to create admin panel menu items for store owners to access the lists and pages. We are going to look into the step by step process involved in creating custom menu item for the Magento 2 admin panel.

Connect to your web server using Filezilla and create a folder Velanapps in the following directory and enter into it:

<magento_docroot>/app/code/

Create a folder named Adminmenu inside the Velanapps folder such that your folder structure is as the follows:

<magento_docroot>/app/code/Velanapps/Adminmenu/

Create folder named etc inside the Adminmenu folder and enter into it. Now create a file module.xml inside the etc folder and paste the following code into it:

<magento_docroot>/app/code/Velanapps/Adminmenu/etc/module.xml

<?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="Velanapps_Adminmenu" setup_version="1.0.0"></module>

</config>

Create the module registration file "registration.php" in the following directory:

<magento_docroot>/app/code/Velanapps/Adminmenu/registration.php

<?php

\Magento\Framework\Component\ComponentRegistrar::register(

                \Magento\Framework\Component\ComponentRegistrar::MODULE,

                'Velanapps_Adminmenu',

                __DIR__

);

Create a folder named adminhtml inside the etc folder such that the folder structure is as the following:

<magento_docroot>/app/code/Velanapps/Adminmenu/etc/adminhtml/

 

Create the menu.xml inside the adminhtml folder and paste the following code in it:

<magento_docroot>/app/code/Velanapps/Adminmenu/etc/adminhtml/menu.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../Backend/etc/menu.xsd">

    <menu>

                                <add id="Velanapps_Adminmenu::levelzero" title="VelanApps" module="Velanapps_Adminmenu" sortOrder="30" resource="Velanapps_Adminmenu::levelzero"/>           

                                  <add id="Velanapps_Adminmenu::admenu_levelone" title="Level One" translate="title" module="Velanapps_Adminmenu" parent="Velanapps_Adminmenu::levelzero" sortOrder="31" resource="Velanapps_Adminmenu::admenu_manage"/>

                                                  <add id="Velanapps_Adminmenu::admenu_leveltwo1" title="Level Two 1" translate="title" module="Velanapps_Adminmenu" parent="Velanapps_Adminmenu::admenu_levelone" sortOrder="10" action="adminhtml/system_config/edit/section/admenu_activation" resource="Velanapps_Adminmenu::admenu_leveltwo1"/>

                                                  <add id="Velanapps_Adminmenu::admenu_leveltwo2" title="Level Two 2" translate="title" module="Velanapps_Adminmenu" parent="Velanapps_Adminmenu::admenu_levelone" sortOrder="10" action="adminhtml/system_config/edit/section/admenu_settings" resource="Velanapps_Adminmenu::admenu_leveltwo2"/>

                </menu>

</config>

Magento 2 Admin Menu Module Folder Structure

Execute the following commands in sequence:

php bin/magento setup:upgrade

php bin/magento setup:di:compile

php bin/magento cache:flush

php bin/magento cache:clean

php bin/magento indexer:reindex

You can check if the module is successfully enabled by executing the module status cli command:

php bin/magento module:status Velanapps_Adminmenu

Description for attributes:

id - Unique identifier of the node

title - Menu item title

module - Name of the module

parent - ID of the parent menu item

sortOrder - Menu item's sort order number

action - URL that should be loaded when user clicks on the menu item

resource - To identify which admin user can access this menu item. Used for ACL purpose.

Now you can login to the Magento 2 admin panel and you could see the following output:

Magento 2 Admin Menu Screen

Write Your Comment

Only registered users can write comments. Please, log in or register