Magento – Change from frontend area to adminhtml area

areamagento2state

I try to use an email template which located in adminhtml area from frontend, the problem is i dont know if changing the state area code is the right way because i alway got an error by doing like this:

      $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
      $area = $objectManager->get('Magento\Framework\App\State');
      $currentAreaCode = $area->getAreaCode();
      $area->setAreaCode(\Magento\Framework\App\Area::AREA_ADMINHTML);

1 exception(s): Exception #0
(Magento\Framework\Exception\LocalizedException): Area code is already
set

Best Answer

You can also try to emulate adminhtml area code by using emulateAreaCode from Magento\Framework\App\State . Check this class implementation as reference: Magento\Widget\Controller\Adminhtml\Widget\Instance\Blocks. I think it's a better solution than just silently ignoring the exception.

Related Topic