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 Custom Menu Item In Magento 2 Admin Menu
In this blog, we will study how to create an item in the admin menu of Magento 2.
if we take a look at the menu structure of Magento 2, There is a parent menu item inside a sub-menu item and inside this sub-menu item, there are is another sub-menu item. that explains the structure of the menu in Magento 2.
To start this process first of all we will show you the admin menu, how it looks.
Structure example:
- Go to Admin panel > Content > Design >Configuration.
There are few steps to create a menu item.
- Create a menu.xml file.
- Add menu items.
Step#1: Create menu.xml:
go to the below directory structure and create a file named menu.xml
File path:
app/code/TOH/HelloWorld/etc/adminhtml/menu.xml
in the menu.xml file paste the below code.
<?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>
.....
</menu>
</config>
Step#2: Add menu items:
To create menu items in the custom menu paste the below code inside the menu tag<menu>.
<add id="TOH_HelloWorld::helloworld" title="Hello World" module="TOH_HelloWorld" sortOrder="51"
resource="TOH_HelloWorld::helloworld"/>
<add id="TOH_HelloWorld::post" title="Manage Posts" module="TOH_HelloWorld" sortOrder="10"
action="TOH_helloworld/post" resource="TOH_HelloWorld::post" parent="TOH_HelloWorld::helloworld"/>
The First item is a parent item and the next item is a sub-menu item and the above code we have created one parent item and one sub-menu item.
if you want to add more items to the same menu add the code which is mentioned below.
<add id="TOH_HelloWorld::post" title="Manage Posts" module="TOH_HelloWorld" sortOrder="10"
action="TOH_helloworld/post" resource="TOH_HelloWorld::post" parent="TOH_HelloWorld::helloworld"/>
- id: attribute is the unique identity of the menu and also should follow the format {Vendor_ModuleName}::{menu_description}.
- title: attribute will show on the menu bar.
- module: attribute will define your module where you creating your menu.
- sort order: attribute describes the level of your menu item. where the menu item is located in the menu.
- parent: attribute is used to create the sub-menu items or sub-sub-menu items.
- action: attribute describes the URL of your page{router_name}{controller_folder}{action_name} when you click on the menu item it will redirect you to that page.
- resources: attribute is used to describe the ACL(Access Control List ) rule, which users have access to use this menu.
Now you need to run a command in the terminal to flush the Magneto cache :
php bin/magento cache:clean
if you need any kind of help you can visit Magento 2 Services.