How to get Magento 2 base URL?

39

11

In Magento 1 Mage::getBaseUrl();, but in Magento 2 I have to pass responsible class object type in constructor.

I don’t have the idea which class I have to pass?

sivakumar

Posted 2015-10-02T02:18:22.213

Reputation: 229

Answers

75

In magento 2.

If you want to get Base url ,then you can try below code:

$this->_storeManager->getStore()->getBaseUrl()

this above code will given you result

http://www.example.com (If Seo rewrite is enable)

And http://www.example.com/index.php (If Seo rewrite is not enable)

If you want Base url without index.php

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

See in details at magento2 get base url and media url and static url

Using Object Manager

Base Url:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$storeManager = $objectManager->get('\Magento\Store\Model\StoreManagerInterface');
$storeManager->getStore()->getBaseUrl();

Base Url without index.php

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

For getting media base url:

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

for getting link url:

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

Edit

For getting the $this->_storeManager You should call inject \Magento\Store\Model\StoreManagerInterface $storeManager

at __construct( ) function at block class

just like :

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

Updated:

Also,you can get base url directly at phtml using direct call of object Manager.

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

Note: Directly call of object manager is not good idea. If you want base url at phtml then inject StoreManagerInterface at block

Amit Bera

Posted 2015-10-02T02:18:22.213

Reputation: 48 624

2I would highly discourage the use of the objectmanager directly, like mentioned in this answer. You should do this by injecting the StoreManager into your Block class, like also mentioned in this answer. – 7ochem – 2016-12-01T10:50:01.920

@7ochem, it is depends on developer call :) – Amit Bera – 2016-12-01T10:56:09.600

2True, I still would highly discourage developers to do so – 7ochem – 2016-12-01T10:58:04.027

24

Simply use this command with you are using a class that extends \Magento\Framework\View\Element\Template.

$this->getBaseUrl()

If not, use this:

$this->_storeManager->getStore()->getBaseUrl()

Rafael Corrêa Gomes

Posted 2015-10-02T02:18:22.213

Reputation: 3 192

Short and efficient – Asish Hira – 2016-04-20T06:44:40.603

Nice, thanks you. Do you know if this needs escaping or not? Magento don't seem to have verified it yet. – Ben Crook – 2016-06-13T09:39:45.293

@Ben-Space48 Do not need scape, such as Magento 1. – Rafael Corrêa Gomes – 2016-06-13T23:57:19.870

1This answer could really use some more context. You're assuming that they are within a class that extends \Magento\Framework\View\Element\Template. However the OP's question makes no reference to where he is currently coding from. Such as Model, Helper, Controller, etc. – Meogi – 2017-04-10T13:12:06.630

6

In Magneto2: The way to get link Url in PHTML is:

echo $this->getUrl('about-us')

Hope it will work....

Chandra Prakash Gupta

Posted 2015-10-02T02:18:22.213

Reputation: 91

5

If you just want to get a URL from your Magento install's root directory you can just use getUrl. It inherits from the AbstractBlock class (Magento\Framework\View\Element\AbstractBlock) so you are able to use it an any of your blocks. Here is an example

$this->getUrl('pub/media/video/', ['_secure' => $this->getRequest()->isSecure()]).$fileName

The first parameter is the path you want, and the second sets the _secure option if the user is browsing over https. You can add to the path by concatenating a specific filename onto the getUrl call or you could add it to the first parameter. The path is relative to the root directory of your Magento install.

Nathan Toombs

Posted 2015-10-02T02:18:22.213

Reputation: 869

1why $this->getUrl('pub/media/catalog/product') this give me path upto catalog directory and ignoring product directory? – chirag dodia – 2015-12-23T14:05:03.260

3

Well if it's Magento 2.0.0. CE Stable version and any "Context" type of object is loaded already in Block class like Magento\Backend\Block\Widget\Context then just call getStoreManager()->getStore()->getBaseUrl() function like below:

$context->getStoreManager()->getStore()->getBaseUrl()

inside the constructor also you can pass arguments like \Magento\Framework\UrlInterface::URL_TYPE_MEDIA inside this getBaseUrl() function.

Hope this helps.

Vicky Dev

Posted 2015-10-02T02:18:22.213

Reputation: 1 142

1

The correct way to get media in PHTML is:

$block->getViewFileUrl('images/myimage.png');

Arvind07

Posted 2015-10-02T02:18:22.213

Reputation: 661

1

In your magento root create Test.php file.

use Magento\Framework\App\Bootstrap;
include('app/bootstrap.php');
$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$storeManager = $objectManager->get('Magento\Store\Model\StoreManagerInterface'); 
$baseUrl= $storeManager->getStore()->getBaseUrl();

Jitendra Mohanta

Posted 2015-10-02T02:18:22.213

Reputation: 434

1

In your block class file add following function:

public function getImageUrl($link_url = '')
    {
        if(!empty($link_url))
        {
            $media_url = $this->_storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);

            return $media_url.'/'.$link_url;
        }
        else
        {
            return '#';
        }
    }

And call this from your .phtml template file with following:

$block->getImageUrl('<relative image path>')

Nahid

Posted 2015-10-02T02:18:22.213

Reputation: 410

1

inject store manager and simply get base url

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


$this->_storeManager->getStore()->getBaseUrl();

Note: don't create object manager always inject

Surendra Kumar Ahir

Posted 2015-10-02T02:18:22.213

Reputation: 440

0

You can get Magento2 Base url by using this:

$this->_storeManager->getStore()->getBaseUrl()

Pratik Kamani

Posted 2015-10-02T02:18:22.213

Reputation: 653

0

Simply: <?php $block->getBaseUrl() ?>

Prince

Posted 2015-10-02T02:18:22.213

Reputation: 4 381

0

In magento 2.

If you want to get Base url ,then you can use the below code:

$this->_storeManager->getStore()->getBaseUrl()

By using objectManager ,you can use the following code

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

$storeManager = $objectManager>get('\Magento\Store\Model\StoreManagerInterface');

$storeManager->getStore()->getBaseUrl();

simya sreejith

Posted 2015-10-02T02:18:22.213

Reputation: 1

-2

Here is the detailed tutorial i found to get the base url and other ones in Magento2. http://www.webmull.com/magento-2-getbase-url/

public function getBaseUrl()
{
    return $this->_storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);
}

Gaurav Jain

Posted 2015-10-02T02:18:22.213

Reputation: 427

this post describes how to get url in magento 1.x, in the magento 2 logic to get url are different. – joni jones – 2015-10-02T07:48:00.547

2no, there is description for magento 2 also, but previous answer by Amit Bera here much more detailed and not just a link, should be accepted as correct one. – FireBear – 2015-10-02T10:31:02.870