0

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

But not matter what I try,I only get https://127.0.0.1/magento

I want to get https://127.0.0.1

I tried

$this->_storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_WEB)

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 "http://127.0.0.1";

    }
2

3 Answers 3

1

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.

0

Try this

\Magento\Framework\App\ObjectManager::getInstance()->get('Magento\Store\Model\StoreManagerInterface')->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_WEB);

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

2
  • 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
0

try use this code:

$this->getBaseUrl()

or

$_SERVER['HTTP_HOST']
1

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.