The Magento 2 store admin gets additional privileges for efficiently fulfilling their responsibility as a store admin.
For example, the admin gets the facility to use a particular shipping or payment method while placing an order from the backend.
However, to implement admin-exclusive features, you need to first check if the current area is frontend or backend in Magento 2. The method given below allows checking if it is frontend or backend.
Check the area of the current URL in Magento 2 with any of the two methods:
1. With objectManager
2. With class method
And, you get one of these outputs as frontend, adminhtml, or webapi_rest.
Method to check if the current area is frontend or backend in Magento 2:
1. With objectManager
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $state = $objectManager->get('Magento\Framework\App\State'); echo $state->getAreaCode();
2. With class method
<?php namespace Vendor\Extension\Block; use Magento\Framework\App\State; use Magento\Framework\View\Element\Template; use Magento\Framework\View\Element\Template\Context; class Index extends Template { protected $state; public function __construct(Context $context, State $state array $data = []) { parent::__construct($context, $data); $this->state = $state; } public function getArea() { return $this->state->getAreaCode(); } } ?>
That’s it.
Any doubts?
Do share the solution with fellow developers via social media.
Thank you.