Magento – Magento 2 get Area Code

magento2plugin

I'm trying to determine on a plugin if the request comes from admin or for frontend.
I had to make a plugin for Magento\Framework\App\Config\ScopeConfigInterface, and in my plugin I need to detect the area code on my aroundGetValue function.
I'm trying this:

$om = \Magento\Framework\App\ObjectManager::getInstance();
$area = $om->get('Magento\Framework\App\State');
$areaCode= $area->getAreaCode();

But when I deploy my code I'm getting this error:

[Magento\Framework\Exception\LocalizedException]
  Area code is not set

I don't know how to check at that level if the request comes from admin or for frontend.
Can anyone help me with this?

Best Answer

On your construct, put:

public function __construct(\Magento\Framework\App\State $state)
{
   $state->setAreaCode(\Magento\Framework\App\Area::AREA_GLOBAL);
}

And try again

Related Topic