How to check programmatically if you are in the admin panel in Magento 2
Unfortunately, Magento 2 offers a more complicated solution for checking whether you are in the admin panel.
In case of Magento 1.x, you should use the following line of code:
1 |
'admin' === Mage::app()->getStore()->getCode() |
For Magento 2 the same operation does not look so simple:
1 2 3 4 5 6 7 8 9 10 11 |
/** * @param int|string|null|bool|\Magento\Store\Api\Data\StoreInterface $store [optional] * @return bool */ function df_is_admin($store = null) { /** @var \Magento\Framework\ObjectManagerInterface $om */ $om = \Magento\Framework\App\ObjectManager::getInstance(); /** @var \Magento\Framework\App\State $state */ $state = $om->get('Magento\Framework\App\State'); return 'adminhtml' === $state->getAreaCode(); } |