2

I want to add my custom menu in admin and also want to add related controller for that.

So how can I do that?

7

In Magento 2 you need to create menu.xml file in your module's etc/adminhtml folder. Here is example of menu.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../Backend/etc/menu.xsd">
    <menu>
        <add id="Lapisbard_StoreLocator::storelocator" title="Lapisbard" module="Lapisbard_StoreLocator" sortOrder="90" resource="Lapisbard_StoreLocator::storelocator" />
        <add id="Lapisbard_StoreLocator::locations" title="Store Locations" module="Lapisbard_StoreLocator" sortOrder="10" parent="Lapisbard_StoreLocator::storelocator" action="lapisbard_storelocator/locations" resource="Lapisbard_StoreLocator::locations"/>
    </menu>
</config>

id should be unique, resource will be used in your acl and using parent you will specify in which menu/sub-menu your custom menu should appear. action="lapisbard_storelocator/locations" specifies where it will be re-directed.

Example of acl.xml which will be present in etc folder.

<?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="Lapisbard_StoreLocator::storelocator" title="Lapisbard" sortOrder="10" >
                        <resource id="Lapisbard_StoreLocator::locations" title="Store Locations" sortOrder="40">
                        </resource>
                    </resource>
            </resource>
        </resources>
    </acl>
</config>

In etc/adminhtml folder you will be having routes.xml. Example:

<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="lapisbard_storelocator" frontName="lapisbard_storelocator">
            <module name="Lapisbard_StoreLocator" />
        </route>
    </router>
</config>

Here frontName="lapisbard_storelocator" is used to specify cotroller's frontname (lapisbard_storelocator is frontname here).

| improve this answer | |
1

In Magento 1, menu configuration are located inside etc/adminhtml.xml but in Magento 2 menu configuration are located in etc/adminhtml/menu.xml file

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../Backend/etc/menu.xsd">
    <menu>
        <add id="Ves_HelloWorld::brand" title="Ves Hello World" module="Ves_HelloWorld" sortOrder="30" resource="Ves_HelloWorld::brand"/>

        <add id="Ves_HelloWorld::brand_head_manage" title="Brand" module="Ves_HelloWorld" sortOrder="10" parent="Ves_HelloWorld::brand" resource="Ves_HelloWorld::brand_head_manage"/>

        <add id="Ves_HelloWorld::brand_new" title="Add New Brand" module="Ves_HelloWorld" sortOrder="10" parent="Ves_HelloWorld::brand_head_manage" action="veshelloworld/brand/new" resource="Ves_HelloWorld::brand_new"/>

        <add id="Ves_HelloWorld::brand_head_settings" title="Settings" module="Ves_HelloWorld" sortOrder="20" parent="Ves_HelloWorld::brand_head_manage" resource="Ves_HelloWorld::brand_manage"/>

        <add id="Ves_HelloWorld::configuration" title="Configuration" module="Ves_HelloWorld" sortOrder="6"
            parent="Ves_HelloWorld::brand_head_settings" action="adminhtml/system_config/edit/section/vesbrand" resource="Ves_HelloWorld::configuration"/>
    </menu>
</config>
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.