How To Check If The Current Area Is Frontend Or Backend In Magento 2

There are two methods to check if the current area is frontend or backend in Magento 2

with Object Manager

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$state = $objectManager->get('Magento\Framework\App\State');
echo $state->getAreaCode();

With Class Method

<?php
namespace VendorName\ModuleName\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();
}
}
?>
We are providing the best Magento 2 Extensions at the lowest prices.
Related Products