15/08/2022
We use cookies to make your experience better. To comply with the new e-Privacy directive, we need to ask for your consent to set the cookies. Learn more.
We use cookies to make your experience better. To comply with the new e-Privacy directive, we need to ask for your consent to set the cookies. Learn more.
There are two methods to check if the current area is frontend or backend in Magento 2
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$state = $objectManager->get('Magento\Framework\App\State');
echo $state->getAreaCode();
<?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();
}
}
?>