70

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?

13 Answers 13

139

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

4
  • 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
    Commented Dec 1, 2016 at 10:50
  • @7ochem, it is depends on developer call :)
    – Amit Bera
    Commented Dec 1, 2016 at 10:56
  • 3
    True, I still would highly discourage developers to do so
    – 7ochem
    Commented Dec 1, 2016 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
    Commented Sep 21, 2020 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()
6
  • 1
    Short and efficient
    – Asish Hira
    Commented Apr 20, 2016 at 6:44
  • 1
    Nice, thanks you. Do you know if this needs escaping or not? Magento don't seem to have verified it yet.
    – Ben Crook
    Commented Jun 13, 2016 at 9:39
  • 1
    @Ben-Space48 Do not need scape, such as Magento 1. Commented Jun 13, 2016 at 23:57
  • 1
    This 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. Commented Apr 10, 2017 at 13:12
  • 1
    In a phtml template you should use $block->getBaseUrl() instead of $this->getBaseUrl() Commented Aug 9, 2018 at 14:44
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

1
  • This is not the Base URL Commented Dec 27, 2022 at 19:55
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

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.

1
  • 1
    why $this->getUrl('pub/media/catalog/product') this give me path upto catalog directory and ignoring product directory? Commented Dec 23, 2015 at 14:05
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.

2

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

1

The correct way to get media in PHTML is:

$block->getViewFileUrl('images/myimage.png');
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->urlBuilder->getUrl(
                'some/path/here',
                [
                   '_secure' => true, // optional
                   '_type' => \Magento\Framework\UrlInterface::URL_TYPE_WEB
                ]
            );
    }
}
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>')
-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();
-1

You can get Magento2 Base url by using this:

$this->_storeManager->getStore()->getBaseUrl()
-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();

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