Back to Top

create admin menu and controller in magento2

Updated 22 February 2024

Here we create admin menu and controller in magento2 .

In previous blog we learn how to Create hello module in magento2 .

now we create menu and controller in Magento2 admin panel. For this we follow our previous blog Create hello module in magento2 .

Before starting the code section, let us create the directory structure that we will need for admin menu and controller.

*Note:- for complete module also include directory structure of blog Create hello module in magento2 .

Searching for an experienced
Magento 2 Company ?
Find out More

app/code/Webkul/Hello/etc/adminhtml
app/code/Webkul/Hello/Controllers/Adminhtml/Employee

as our previous blog Create hello module in magento2 we have already a working hello module and now we add admin menu and controller in that module.

Now, as we have the directory structure ready, we will now create file as per module requirement in given sequence:

In Magento 1, menu configuration are locate inside adminhtml.xml but in Magento 2 menu configuration is locate menu.xml

1.First, we have to create the menu configuration file named menu.xml in app/code/Webkul/Hello/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>
        <add id="Webkul_Hello::manager" title="Webkul Menu" module="Webkul_Hello" sortOrder="10" resource="Webkul_Hello::manager"/>
        <add id="Webkul_Hello::employee" title="Employee Manager" module="Webkul_Hello" sortOrder="0" parent="Webkul_Hello::manager"
                action="hello/employee" resource="Webkul_Hello::employee"/>
    </menu>
</config>

Done!

Then clear magento cache and go to admin panel. You add new menu, your new menu is appended with(Webkul Menu-> Employee Manager) as following.

2.Now, we have to create the Admin Controller  file.In Magento2, we need to create separate file for each action of admin under the Controller/Adminhtml folder.

file named Index.php in app/code/Webkul/Hello/Controller/Adminhtml/Employee

<?php
namespace Webkul\Hello\Controller\Adminhtml\Employee;

class Index extends \Magento\Backend\App\Action
{
    /**
     * Hello test controller page.
     *
     * @return \Magento\Backend\Model\View\Result\Page
     */
    public function execute()
    {
        echo __('Hello Webkul Team.');
    }

    /**
     * Check Permission.
     *
     * @return bool
     */
    protected function _isAllowed()
    {
        return $this->_authorization->isAllowed('Webkul_Hello::employee');
    }
}

3. Finally, we will create a route configuration file for admin  named routes.xml in app/code/Webkul/Hello/etc/adminhtml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
    <router id="admin">
        <route id="hello" frontName="hello">
            <module name="Webkul_Hello" />
        </route>
    </router>
</config>

4. Now our admin controller is ready when you click your new menu (Webkul Menu >> Employee Manager) then your controller run and output display as following

output

Download sample code form github

Thanks ­čÖé

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


5 comments

  • Sayyed Huma Naqvi
    • saachi
  • saachi
  • Bilal
  • James K
  • Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home