Skip to main content

Magento 2 - Add to Admin Menu


In magento 2, the admin menu has changed from top to left side. There is not much difference between the old way and the new way to add the menu. Just some file changes and way to write them. You can find the sample code in the git repo.

Create a module as per the blog post. The base folder structure to create a menu in admin looks like below:
VendorName
    ModuleName
        Controller
            Adminhtml
                Index
                    Index.php
        etc
            adminhtml
                menu.xml
                routes.xml
            acl.xml
            module.xml

The code inside routes.xml will look as below. This gives the admin route details. Most of them as similar to the magento 1 code.
<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
    <router id="admin">
        <route id="vendorname_samplemodule" frontName="sample_module">
            <module name="VendorName_SampleModule" before="Magento_Backend" />
        </route>
    </router>
</config>

The code inside menu.xml will look as below. The below code adds a menu as "VendorName > My Tab > Sample Page".
<?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="VendorName_SampleModule::base" title="VendorName" module="VendorName_SampleModule" sortOrder="55" dependsOnModule="VendorName_SampleModule" resource="VendorName_SampleModule::base"/>
        <add id="VendorName_SampleModule::mytab" title="My Tab" module="VendorName_SampleModule" sortOrder="10" parent="VendorName_SampleModule::base" resource="VendorName_SampleModule::mytab"/>
        <add id="VendorName_SampleModule::index" title="Sample Page" module="VendorName_SampleModule" sortOrder="20" parent="VendorName_SampleModule::mytab" action="vendorname_samplemodule/index/sample" resource="VendorName_SampleModule::view"/>
    </menu>
</config>

The code inside acl.xml will look as below. The code is similar to magento 1. Here, there is id, which is used for all the future references. No need to remember the complete path of acl.
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Acl/etc/acl.xsd">
    <acl>
        <resources>
            <resource id="Magento_Backend::admin">
                <resource id="VendorName_SampleModule::base" title="VendorName" sortOrder="55">
                    <resource id="VendorName_SampleModule::mytab" title="My Tab" sortOrder="20">
                        <resource id="VendorName_SampleModule::view" title="Sample Page" sortOrder="20" />
                    </resource>
                </resource>
            </resource>
        </resources>
    </acl>
</config>

The code inside the controller file Index.php will look as below. These are the basic code required to show a page. The block file will have the content to show on the page. It can be grid, form, custom phtml etc.
<?php

namespace VendorName\SampleModule\Controller\Adminhtml\Index;

use Magento\Backend\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;

class Sample extends \Magento\Backend\App\Action {

    protected $_resultPageFactory;

    public function __construct(Context $context, PageFactory $resultPageFactory) {
        $this->_resultPageFactory = $resultPageFactory;
        parent::__construct($context);
    }

    public function execute() {
        $resultPage = $this->_resultPageFactory->create();
        $resultPage->setActiveMenu('VendorName_SampleModule::index');
        $resultPage->getConfig()->getTitle()->prepend(__('My Sample Page'));
        return $resultPage;
    }

    protected function _isAllowed() {
        return $this->_authorization->isAllowed('VendorName_SampleModule::view');
    }

}



Comments

Popular posts from this blog

Magento 2 - Create Attributes in Setup

Sometimes in our module we require to create product or category attributes automatically on module install. Also, we might require to add extra fields to quote, sales order, invoice, creditmemo tables. We will go through how to do that in magento 2. You can find the sample code in the git repo .