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.
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(); } }
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!
Generating image thumbnails is a common requirement in web applications, especially when handling media-heavy content.…
In today’s digital landscape, web application security is paramount. As a powerful PHP framework, Laravel…
October was an exciting month for MageComp! From significant updates across our Magento 2 extension…
In modern web development, seamless navigation and state management are crucial for delivering a smooth…
Magento Open Source 2.4.8 beta version released on October 8, 2024. The latest release of…
Hello Magento Friends, Creating catalog price rules programmatically in Magento 2 can be a valuable…