How to create Magento 2 custom system configuration and controller
In Magento 2 menu configurations are located inside menu.xml, so to create custom system configuration and controller you should:
1. Create the menu.xml file in app/code/Webkul/Hello/etc/adminhtml (the tip is based on Webkul’s post):
1 2 3 4 5 6 |
<?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> |
2. Now, it’s time to clear cache. After you’ve performed this procedure, go to the admin panel, and add a new menu Content -> Webkul Menu.
3. Create the Admin Controller file. Please note that in Magento 2, each action requires a separate file under the Controller/Adminhtml folder. Your file
dubbed Index.php is in app/code/Webkul/Hello/Controller/Adminhtml/Employee:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?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"); } } ?> |
4. Now, generate a route configuration file, routes.xml, for admin under app/code/Webkul/Hello/etc/adminhtml:
1 2 3 4 5 6 7 8 |
<?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> |