Hello Everyone,
In this blog, we will learn about how to Add Custom Tab on the Admin Dashboard in Magento 2.
Creating a Custom Tab on Dashboard will be useful to display some specific information relevant to your business.
Without wasting your time, let us guide you straight away. Follow the easy step given below to Add Custom Tab on Admin Dashboard in Magento 2.
STEPS FOR ADD CUSTOM TAB ON ADMIN DASHBOARD IN MAGENTO 2
Step 1: Create file routes.xml
app/code/Vendor/Extension/etc/adminhtml/routes.xml
<?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="helloworld" frontName="helloworld">
<module name="Vendor_Extension" before="Magento_Backend"/>
</route>
</router>
</config>
Step 2: Create file MagecuriousCustomTab.php
app/code/Vendor/Extension/Controller/Adminhtml/Dashboard/MagecuriousCustomTab.php
<?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 MagecuriousCustomTab extends AjaxBlock
{
protected $_resultPageFactory;
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: Create file custom_file.phtml
app/code/Vendor/Extension/view/adminhtml/templates/custom_file.phtml
<div>
<?php echo __("Magecurious Tab");?>
</div>
Step 4: Create file di.xml
app/code/Vendor/Extension/etc/adminhtml/di.xml
<?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: Create file Grids.php
app/code/Vendor/Extension/Block/Dashboard/Grids.php
<?php
namespace Vendor\Extension\Block\Dashboard;
class Grids extends \Magento\Backend\Block\Dashboard\Grids
{
protected function _prepareLayout()
{
parent::_prepareLayout();
$this->addTab(
'custom_tab',
[
'label' => __('Magecurious Custom Tab'),
'url' => $this->getUrl('helloworld/dashboard/magecuriouscustomtab', ['_current' => true]),
'class' => 'ajax',
'active' => false
]
);
}
}
Step 6: Finally run the below commands
$ php bin/magento setup:upgrade
$ php bin/magento cache:clean
$ php bin/magento cache:flush
Step 7: Output:
Final Thoughts:
So this was the easiest way which we have told you in this blog. This is how you can Add Custom Tab on Admin Grid in Magento 2. Hope you liked the blog.
So quickly go to the comment box and tell me how you like this blog?
Stay tuned with us on our site to get new updates of Magento.
Thanks for reading and visiting our site.
