I am trying to get the base url of my website.

But not matter what I try,I only get

I want to get

I tried


and all the associated techniques with object manage but always give the same result.

namespace Vendor\Module\Block;

use Magento\Framework\View\Element\Template;
use Magento\Framework\View\Element\Template\Context;
//use Vendor\Module\Model\FidoServiceFactory;
use Magento\Customer\Model\Session;

class Register extends Template
    private $fidoCallerFactory;
    private $session;
    // public function __construct(Context $context, FidoServiceFactory $fidoServiceFactory, Session $session) {
    //     parent::__construct($context, []);
    //     $this->fidoServiceFactory = $fidoServiceFactory;
    //     $this->session = $session;
    // }
    // #Returns the base URL of currently visited store.
    public function getBaseUrl() {
    //  console.log($this->_storeManager->getStore()->getBaseUrl());
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $storeManager = $objectManager->get('\Magento\Store\Model\StoreManagerInterface');
        return $this->_storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_LINK);
      //return "";


3 Answers 3


I am posting this so that beginners in Magento will not get confused again. If you use getbaseurl in localhost, you will also get the name of the directory that you are in.

This won't be affecting while you migrate to a live server.

You can easily use rtrim to get rid of the name of the directory.


Try this


I think you are using wrong variable for calling object manager. you have created object manager in variable $storeManager and used $this->_storeManager

  • It gave the same answer :( Jan 23, 2019 at 12:54
  • Have you tried frontend deployment? I dont know if it is necessary in your case, but give it a try, and clear cache. I have used this code in my block and it's working Jan 24, 2019 at 4:39

try use this code:




Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.