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 .
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="../../../../Magento/Backend/etc/menu.xsd"> <menu> <add id="Webkul_Hello::Employee" title="Webkul Menu" module="Webkul_Hello" sortOrder="100" parent="Magento_Backend::content_elements" 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(Content -> Webkul Menu) 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{ /** * Index Action for Employee * @return Void * */ public function execute(){ die("hello Webkul 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="../../../../../../lib/internal/Magento/Framework/App/etc/routes.xsd"> <router id="admin"> <route id="hello" frontName="hello"> <module name="Webkul_Hello" /> </route> </router> </config>