4

I was wondering how to use the getBaseUrl() in controller/helper class..

anybody have idea ?

Mohammad Mujassam
  • 2,170
  • 7
  • 26
  • 29

1 Answers1

15

Based on the duplicate question, I'm just going to explain how to do it in an helper/controller

First you have to inject the Magento\Store\Model\StoreManagerInterface in your constructor:

protected $_storeManager;

public function __construct(
    ...
    \Magento\Store\Model\StoreManagerInterface $storeManager
) {
    $this->_storeManager = $storeManager;
    parent::__construct(...);
}

Then you can use it whenever you want in your class like this:

$this->_storeManager->getStore()->getBaseUrl();
Raphael at Digital Pianism
  • 70,385
  • 34
  • 188
  • 352