56

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

112

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

  • 3
    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 1 '16 at 10:50
  • @7ochem, it is depends on developer call :) – Amit Bera Dec 1 '16 at 10:56
  • 2
    True, I still would highly discourage developers to do so – 7ochem Dec 1 '16 at 10:58
41

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()

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

$block->getBaseUrl()
  • Short and efficient – Asish Hira Apr 20 '16 at 6:44
  • Nice, thanks you. Do you know if this needs escaping or not? Magento don't seem to have verified it yet. – Ben Crook Jun 13 '16 at 9:39
  • @Ben-Space48 Do not need scape, such as Magento 1. – Rafael Corrêa Gomes Jun 13 '16 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. – Darren Felton Apr 10 '17 at 13:12
  • In a phtml template you should use $block->getBaseUrl() instead of $this->getBaseUrl() – Daniel Kratohvil Aug 9 '18 at 14:44
13

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

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

I hope it will work for you

6

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
    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
6

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

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.

2

The correct way to get media in PHTML is:

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

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

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

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>')
0

You can get Magento2 Base url by using this:

$this->_storeManager->getStore()->getBaseUrl()
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();
-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);
}
  • this post describes how to get url in magento 1.x, in the magento 2 logic to get url are different. – joni jones Oct 2 '15 at 7:48
  • 2
    no, 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 Oct 2 '15 at 10:31

protected by Amit Bera Mar 20 at 16:23

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

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