68

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?

Rafael Corrêa Gomes
  • 13,309
  • 14
  • 84
  • 171
sivakumar
  • 267
  • 10
  • 35
  • 69

13 Answers13

137

In magento 2.

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

/**
* @var \Magento\Store\Model\StoreManagerInterface $this->_storeManager
*/

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

Where $this->_storeManager instance of \Magento\Store\Model\StoreManagerInterface

this above code will give 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
  • 77,456
  • 20
  • 123
  • 237
  • 5
    I 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 Dec 01 '16 at 10:50
  • @7ochem, it is depends on developer call :) – Amit Bera Dec 01 '16 at 10:56
  • 3
    True, I still would highly discourage developers to do so – 7ochem Dec 01 '16 at 10:58
  • @7ochem It's not recommended to use as per Magento, but good to know it exists and how to use it. – Duke Sep 21 '20 at 11:36
63

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

$this->getBaseUrl()

If not, you can use this:

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

Or if you are using it in a PHTML template use:

$block->getBaseUrl()
Rafael Corrêa Gomes
  • 13,309
  • 14
  • 84
  • 171
15

In Magneto2: This is way to get Url link in PHTML file:

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

I hope it will work for you

Shorabh
  • 1,508
  • 12
  • 18
7

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
  • 1,589
  • 3
  • 17
  • 24
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
  • 999
  • 5
  • 12
  • 1
    why $this->getUrl('pub/media/catalog/product') this give me path upto catalog directory and ignoring product directory? – chirag dodia Dec 23 '15 at 14:05
2

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

PЯINCƎ
  • 11,669
  • 3
  • 25
  • 80
2

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
  • 1,992
  • 9
  • 29
  • 51
1

The correct way to get media in PHTML is:

$block->getViewFileUrl('images/myimage.png');
Arvind07
  • 1,229
  • 2
  • 16
  • 18
1

To get the base URL with a path, use the \Magento\Framework\UrlInterface and add \Magento\Framework\UrlInterface::URL_TYPE_WEB as a parameter:

<?php

class Sample { public function __construct( private readonly \Magento\Framework\UrlInterface $urlBuilder ) {}

public function getUrl() {
    return $this-&gt;urlBuilder-&gt;getUrl(
            'some/path/here',
            [
               '_secure' =&gt; true, // optional
               '_type' =&gt; \Magento\Framework\UrlInterface::URL_TYPE_WEB
            ]
        );
}

}

bassplayer7
  • 2,196
  • 22
  • 34
0

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
  • 621
  • 1
  • 7
  • 13
-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
  • 642
  • 8
  • 18
-1

You can get Magento2 Base url by using this:

$this->_storeManager->getStore()->getBaseUrl()
Teja Bhagavan Kollepara
  • 3,816
  • 5
  • 32
  • 69
Pratik Kamani
  • 1,465
  • 2
  • 16
  • 31
-2

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();
Piyush
  • 5,893
  • 9
  • 34
  • 64