
How to get base url in Magento 2
In Magento 2, you can retrieve the base URL using various methods depending on the context in which you need the URL. Here are several ways to get the base URL in Magento 2:
1. Using the Object Manager (Not Recommended):
“`php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$storeManager = $objectManager->get(\Magento\Store\Model\StoreManagerInterface::class);
$baseUrl = $storeManager->getStore()->getBaseUrl();
“`
Note: Directly using the Object Manager is not recommended. It’s better to use dependency injection to retrieve the necessary objects.
2. Using Dependency Injection:
“`php
use Magento\Store\Model\StoreManagerInterface;
class MyClass
{
protected $storeManager;
public function __construct(
StoreManagerInterface $storeManager
) {
$this->storeManager = $storeManager;
}
public function getBaseUrl()
{
return $this->storeManager->getStore()->getBaseUrl();
}
}
“`
Make sure to inject the `StoreManagerInterface` into your class constructor.
3. Using the Context Object:
“`php
use Magento\Framework\App\Http\Context as HttpContext;
class MyClass
{
protected $httpContext;
public function __construct(
HttpContext $httpContext
) {
$this->httpContext = $httpContext;
}
public function getBaseUrl()
{
return $this->httpContext->getValue(\Magento\Framework\App\Http\Context::CONTEXT_BASE_URL);
}
}
“`
The `HttpContext` object provides access to the base URL value.
4. Using the Store Config:
“`php
use Magento\Store\Model\StoreManagerInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;
class MyClass
{
protected $storeManager;
protected $scopeConfig;
public function __construct(
StoreManagerInterface $storeManager,
ScopeConfigInterface $scopeConfig
) {
$this->storeManager = $storeManager;
$this->scopeConfig = $scopeConfig;
}
public function getBaseUrl()
{
$storeCode = $this->storeManager->getStore()->getCode();
return $this->scopeConfig->getValue(‘web/unsecure/base_url’, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $storeCode);
}
}
“`
This method retrieves the base URL from the store configuration.
These are some of the common ways to get the base URL in Magento 2. Choose the method that best suits your requirements and coding standards. It’s recommended to use dependency injection rather than using the Object Manager directly.

Need help? - Get a Quote in under a minute

Stephanie & Joseph are Award Winning London Web Designers at The UK Web Design Company who are ready to help you with your website today.
Just take a couple of seconds to fill out this quick easy form and we will contact you right back
Need help? - Get a Quote in under a minute from the best web designers near you

How to get base url in Magento 2
In Magento 2, you can retrieve the base URL using various methods depending on the context in which you need the URL. Here are several ways to get the base URL in Magento 2:
1. Using the Object Manager (Not Recommended):
“`php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$storeManager = $objectManager->get(\Magento\Store\Model\StoreManagerInterface::class);
$baseUrl = $storeManager->getStore()->getBaseUrl();
“`
Note: Directly using the Object Manager is not recommended. It’s better to use dependency injection to retrieve the necessary objects.
2. Using Dependency Injection:
“`php
use Magento\Store\Model\StoreManagerInterface;
class MyClass
{
protected $storeManager;
public function __construct(
StoreManagerInterface $storeManager
) {
$this->storeManager = $storeManager;
}
public function getBaseUrl()
{
return $this->storeManager->getStore()->getBaseUrl();
}
}
“`
Make sure to inject the `StoreManagerInterface` into your class constructor.
3. Using the Context Object:
“`php
use Magento\Framework\App\Http\Context as HttpContext;
class MyClass
{
protected $httpContext;
public function __construct(
HttpContext $httpContext
) {
$this->httpContext = $httpContext;
}
public function getBaseUrl()
{
return $this->httpContext->getValue(\Magento\Framework\App\Http\Context::CONTEXT_BASE_URL);
}
}
“`
The `HttpContext` object provides access to the base URL value.
4. Using the Store Config:
“`php
use Magento\Store\Model\StoreManagerInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;
class MyClass
{
protected $storeManager;
protected $scopeConfig;
public function __construct(
StoreManagerInterface $storeManager,
ScopeConfigInterface $scopeConfig
) {
$this->storeManager = $storeManager;
$this->scopeConfig = $scopeConfig;
}
public function getBaseUrl()
{
$storeCode = $this->storeManager->getStore()->getCode();
return $this->scopeConfig->getValue(‘web/unsecure/base_url’, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $storeCode);
}
}
“`
This method retrieves the base URL from the store configuration.
These are some of the common ways to get the base URL in Magento 2. Choose the method that best suits your requirements and coding standards. It’s recommended to use dependency injection rather than using the Object Manager directly.
Need help? - Get a Quote in under a minute

Need help? - Get a Quote in under a minute

Stephanie & Joseph Award Winning London Web Designers at
The UK Web Design Company are ready to help you with your website
Just take a couple of seconds to fill out this quick easy form and we will contact you right back
Need help? - Get a Quote in under a minute from the best web designers near you