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

Generating Thumbnails with Spatie Media Library in Laravel 11: A Step-by-Step Guide

Generating image thumbnails is a common requirement in web applications, especially when handling media-heavy content.…

16 hours ago

Enhancing Web Application Security with Laravel’s Built-In Features

In today’s digital landscape, web application security is paramount. As a powerful PHP framework, Laravel…

2 days ago

Magento 2 Extensions Digest October 2024 (New Release & Updates)

October was an exciting month for MageComp! From significant updates across our Magento 2 extension…

2 days ago

Improving Error Handling and Transition Management in Remix with useRouteError and useViewTransitionState

In modern web development, seamless navigation and state management are crucial for delivering a smooth…

1 week ago

Magento Open Source 2.4.8-Beta Release Notes

Magento Open Source 2.4.8 beta version released on October  8, 2024. The latest release of…

2 weeks ago

How to Create Catalog Price Rule in Magento 2 Programmatically?

Hello Magento Friends, Creating catalog price rules programmatically in Magento 2 can be a valuable…

2 weeks ago