How To

Magento 2: How to Add Custom Tab on Admin Dashboard

Hello Magento Friends,

In today’s blog, we will learn How to Add Custom Tab on Admin Dashboard in Magento 2.

Creating a custom tab on the admin dashboard in Magento 2 can be a useful way to display specific information or tools that are relevant to your business. Here’s a step-by-step guide on how to add a custom tab on the Magento 2 admin dashboard:

Steps to Add Custom Tab on Admin Dashboard in Magento 2:

Step 1: First, we need to create a “routes.xml” file inside the extension at the following path.

app\code\Vendor\Extension\etc\adminhtml\

Then add the code as follows

<?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="vendor" frontName="vendor">
            <module name="Vendor_Extension" before="Vendor_Extension" />
        </route>
    </router>
</config>

Step 2: After that, we need to create the “Bestvendors.php” file inside the extension at the following path.

app\code\Vendor\Extension\Controller\Adminhtml\Dashboard

And add the below-mentioned code

<?php
namespace Vendor\Extension\Controller\Adminhtml\Dashboard;

use Magento\Backend\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;
use Magento\Backend\Controller\Adminhtml\Dashboard\AjaxBlock;
use Magento\Framework\Controller\Result\RawFactory;
use Magento\Framework\View\LayoutFactory;

class Bestvendors extends AjaxBlock
{
    protected $resultRawFactory;
    protected $layoutFactory;

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

    public function execute()
    {
        $resultPage = $this->_resultPageFactory->create();
        $resultPage->getConfig()->getTitle()->prepend(__('Add Custom Tab'));

        $block = $resultPage->getLayout()
                ->createBlock('Magento\Framework\View\Element\Template')
                ->setTemplate('Vendor_Extension::custom_file.phtml')
                ->toHtml();

        $this->getResponse()->setBody($block);
    }
}

Step 3: After that, we need to create a “custom_file.phtml” file inside the extension at the following path.

app\code\Vendor\Extension\view\adminhtml\templates

Now, add the following piece of code

<div>
     <?php echo __("Hello");?>
</div>

Step 4: After that, we need to create a “di.xml” file inside the extension at the following path.

app\code\Vendor\Extension\etc\adminhtml\

Now add the code as follows

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
    <preference for="Magento\Backend\Block\Dashboard\Grids" type="Vendor\Extension\Block\Dashboard\Grids"/>
</config>

Step 5: After that, we need to create a “Grids.php” file inside the extension at the following path.

app\code\Vendor\Extension\Block\Dashboard\

And include the below code snippet

<?php

namespace Vendor\Extension\Block\Dashboard;

class Grids extends \Magento\Backend\Block\Dashboard\Grids
{
    protected function _prepareLayout() 
    {
        parent::_prepareLayout();
        $this->addTab(
            'best_vendors',
            [
                'label'     => __('Best Vendors'),
                'url'       => $this->getUrl('vendor/dashboard/bestvendors', ['_current' => true]),
                'class'     => 'ajax',
                'active'    => false
            ]
        );
    }
}

Step 6: Once all files are created in your Magento, you need to run Magento upgrade, compile and deploy commands as follows.

php bin/magento setup:upgrade
php bin/magento setup:static-content:deploy -f
php bin/magento setup:di:compile
php bin/magento cache:clean
php bin/magento cache:flush

Output:

“Best Vendor” tab added in the Magento  2 Admin Dashboard.

Conclusion:

Hence, this way, you can add custom tabs in the admin dashboard of your Magento 2 store.

There are other areas of your Magento 2 store where you can add a custom tab:

If you have any doubts regarding the above steps, feel free to connect with me through the comment section. Share the tutorial with your friends if you found it useful. Stay in touch with us for more such Magento 2 tutorials.

Happy Coding!

Click to rate this post!
[Total: 0 Average: 0]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

Recent Posts

Five Essential Payroll Compliance Tips for eCommerce Startups

Are you setting up a payroll system for your eCommerce startup? Ensuring compliance with myriad…

9 hours ago

Optimizing Laravel Blade: Unlocking Advanced Fetcher Techniques

In the expansive universe of Laravel development, Blade serves as the stellar templating engine, propelling…

11 hours ago

Magento 2: Add Quantity Increment and Decrement on Category Page

Hello Magento Friends, In this blog, we will discuss about adding quantity increment and decrement…

2 days ago

How to Integrate ChatGPT with Laravel Application?

In this guide, we'll explore how to integrate ChatGPT, an AI-powered chatbot, with a Laravel…

5 days ago

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

1 week ago

Magento 2 Extensions Digest April 2024 (New Release & Updates)

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

1 week ago