2

I have tried this but this haven't worked for me

{
    if (isset($this->_areaCode)) {
        throw new MagentoFrameworkExceptionLocalizedException(
            new MagentoFrameworkPhrase('Area code is already set')
        );
    }
    $this->_configScope->setCurrentScope($code);
    $this->_areaCode = $code;
}```
1

By catching the exception Magento throws and silently ignoring it.

public function __construct(
    \Magento\Framework\App\State $appState,
    $name=null
) {
    try {
        $appState->setAreaCode(\Magento\Framework\App\Area::AREA_ADMINHTML);
    } catch (MagentoFrameworkExceptionLocalizedException $e) {
        // intentionally left empty
    }
    parent::__construct($name);
}

Or

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$area = $objectManager->get('Magento\Framework\App\State');
if (!$area->getAreaCode()) {
    $area->setAreaCode(\Magento\Framework\App\Area::AREA_ADMINHTML);
}
$currentAreaCode = $area->getAreaCode();
      
| improve this answer | |
  • Using objectManager is not recommended and its considered bad practise. Inject the desired class into your own class and then use it without calling the objectManageron it. – Petar Borisovski Oct 15 at 7:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.