Categories: Magento

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

"`
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

"`
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

"`
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.

Article by: Web Designers Near Me | Author: Joseph Cozens – Senior Website Designer

With over 25 years experience, Joseph started as the leased line manager for an ISP in the 1990’s and built websites for the support team internally, but was often asked by corporate business customers if we could provide a website for their company. So in 1998 with people looking for website designers near me, he started the web design company building websites part time, and in 1999 started building websites full time. In the early 2000’s we decided to call ourselves DesignsOnline.co.uk Joseph built one of the first online medical schools allowing doctors worldwide to enrol and pay for medical statistics training via the internet… Joseph used to build bespoke content management systems for companies who needed to regularly update their websites and for bespoke ecommerce websites which were hand coded at the time.

Today Joseph develops and builds top level professional websites without the needlessly high price tag.
You can contact Joseph directly via WhatsApp on 07944062954.

Web Designers Near Me

With over 25 years experience, Joseph started as the leased line manager for an ISP in the 1990's and built websites for the support team internally, but was often asked by corporate business customers if we could provide a website for their company. So in 1998 with people looking for website designers near me, he started the web design company building websites part time, and in 1999 started building websites full time. In the early 2000's Joseph built one of the first online medical schools allow doctors worldwide to enrol and pay for medical statistics training via the internet.