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?
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);
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; }
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
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()
\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
In Magneto2: This is way to get Url link in PHTML file:
echo $this->getUrl('about-us')
I hope it will work for you
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
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.
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.
The correct way to get media in PHTML is:
$block->getViewFileUrl('images/myimage.png');
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
]
);
}
}
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>')
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();
You can get Magento2 Base url by using this:
$this->_storeManager->getStore()->getBaseUrl()
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();