How to check if the current area is frontend or backend?
How to check if the current area is frontend or backend?

How to check if the current area is frontend or backend?

Use Magento\Framework\App\State class to get the area code of the current page.

I will show you how you can get the current area by object manager and with dependency injection.

With ObjectManager:

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

With Dependency Injection:

protected $_state;

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

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

Here getAreaCode() function returns frontend or adminhtml or webapi_rest

NOTE: Don’t use object manager directly. Read this for more info: https://magento.stackexchange.com/questions/117098/magento-2-to-use-or-not-to-use-the-objectmanager-directly