You can get the base URL of the store in the controller file using Store Manager Interface.
To Retrieve the Base URL of the current store, You need to instantiate constructor in your controller file,
Check below code snippet for getting base URL,
<?php namespace Rbj\BaseUrl\Model; use Magento\Store\Model\StoreManagerInterface; class StoreBaseUrl { /** * @var StoreManagerInterface */ private $storeManager; /** * @param StoreManagerInterface $storeManager */ public function __construct( StoreManagerInterface $storeManager, ) { $this->storeManager = $storeManager; } /* return store url as string */ public function getStoreUrl() { $storeUrl = $this->storeManager->getStore()->getBaseUrl(); return $storeUrl; }
echo $this->getStoreUrl();
Output:
http://magento2.docker/en (With Current store code in multiple store site)
- If you want to only URL without store code,
$storeUrl = $this->storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_WEB);
Output:
http://magento2.docker (without store code at suffix)