Magento 2 – Check if it is Frontend or Backend

adminfrontendmagento2

I want to check whether it is front or backend.
How can I do that?

Best Answer

Read More: blog.mageprince.com

With objectManager

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$state =  $objectManager->get('Magento\Framework\App\State');
echo $state->getAreaCode(); //frontend or adminhtml or webapi_rest

With Dependency Injection

protected $_state;

public function __construct (
    \Magento\Framework\App\State $state
) {
    $this->_state = $state;
}

public function getArea()
{
    return $this->_state->getAreaCode();
}

Note: As per magento2 coding standards don't use object manager instance directly in files

Related Topic