How to Emulate Area in Magento 2

How to Emulate Area in Magento 2

In today’s Magento blog, I am going to explain Emulating Areas in Magento 2.

The simple meaning of emulating is imitating. In Magento 2 while working with frontend and backend if you want to simulate one area with another, at that time the concept of the emulating area is used.

For instance, here I have explained how to get a frontend website logo URL in the admin area using emulation in Magento 2. When there is a need to get the website logo URL in the Magento admin area at that time if we apply the code of the frontend area, we do not get the logo URL. In this situation, we need to use emulating areas to get the frontend logo URL in the Magento Admin area.

Use the below code for Emulating Areas in Magento 2.

How to Emulate Area in Magento 2:

You can do it in any file but here we create a controller to achieve this.

Step 1: Add index.php in the following path

app\code\Vendor\Extension\Controller\Adminhtml\Getlogourl\Index.php

Now add the below code

<?php

namespace Vendor\Extension\Controller\Adminhtml\Getlogourl;

class Index extends \Magento\Backend\App\Action
{
	/**
	 * @var \Magento\Theme\Block\Html\Header\Logo
	 */
	protected $logo;
	
	/**
	 * @var \Magento\Store\Model\App\Emulation
	 */
	protected $emulation;
	
	public function __construct(
		    \Magento\Backend\App\Action\Context $context,
		    \Magento\Theme\Block\Html\Header\Logo $logo,
		    \Magento\Store\Model\App\Emulation $emulation
      )
      {
        $this->_logo = $logo;
        $this->emulation = $emulation;
        parent::__construct($context);
      } 
    
    public function execute()
    {
    	 $storeId = 1;
	    $this->emulation->startEnvironmentEmulation($storeId, \Magento\Framework\App\Area::AREA_FRONTEND, true); // You can set store id and area
	    $logo = $this->_logo->getLogoSrc();
	    $this->emulation->stopEnvironmentEmulation();
    }
}

Conclusion:

This way you can Emulate Area in Magento 2 to get the logo URL in the admin panel. Alternatively, Get Logo URL, Alt Text, Logo Size in Magento 2 to increase brand awareness.

If you have any doubts regarding this, you can inform me via the comment box. I will be happy to solve it for you. Spread the article amongst your friends and remain updated for more such Magento 2 solutions.

Happy Coding!

Previous Article

Black Friday Cyber Monday Mega Magento SALE 2021: Live Now!

Next Article

Magento 2: Get Simple Product from Configurable Product before Add to Cart

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *

Get Connect With Us

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨