How To

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!

Click to rate this post!
[Total: 5 Average: 5]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate ?️ Certified Magento Developer?‍?. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.?

Recent Posts

Why Your Business Needs a SaaS/E-commerce Marketing Agency?

In today’s era, having a solid online presence and effective marketing strategies are vital factors…

3 hours ago

Handling File Uploads in a Remix Application

In this blog, I will explain about handling file uploads in Shopify Remix App. File…

3 hours ago

Upgrade Your E-commerce Store with Magento 2 Hyvä Theme

Magento 2 Hyvä Theme is quickly becoming a popular choice among e-commerce businesses for its…

1 day ago

A Complete Guide of Hyvä Themes

In the rapidly evolving world of e-commerce, the success of an online store greatly hinges…

1 day ago

Magento 2: How to Add Custom Button to Download Custom Created PDF Programmatically in Admin Sales Order View

Hello Magento Friends, Ever wanted to provide admins with a quick way to download custom…

1 day ago

Mastering Tailwind CSS in Laravel: A Comprehensive Guide

Tailwind CSS has emerged as a powerful utility-first CSS framework, offering developers a unique approach…

6 days ago