Thursday, 4 June 2015

magento2 get base url and media url and static url

magento2 get base url and media url and static url. Whenever you are working on  magento2 then a lot of times you need to get base URL and media URL and static url. From this blog, you base url, media url, static content url in Magento2.Magento2 basically, use interface

/* This is an interface Class */
Magento\Store\Model\StoreManagerInterface

And this class have a function  Store()  which is provide store data.

After that,  using getBaseUrl() over Store()  you can get base url  and also get Media URL.

/* Using Direct Object Manager */ 
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
/* Get store manager */ 
$storeManager = $objectManager->get('\Magento\Store\Model\StoreManagerInterface');
// BASE URL 
$baseUrl = $storeManager->getStore()->getBaseUrl();

// MEDIA URL 
$mediaUrl =$storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);
// Static contnt URL 

$statiContenteUrl = $storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_STATIC);

Where $storeManager is the instance of Magento\Store\Model\StoreManagerInterface.

If you want to get Base Url without Index.php then click here.

For Getting Media Url Use Below code:

$mediaUrl =$storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);

Get Static content Url

$statiContenteUrl =$storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_STATIC);

But I am strongly recommended to NOT use object Manager. Use Interface injection at the __construct function of your class. Like below example:

     /* @var \Magento\Store\Model\StoreManagerInterface
     */
    protected $_storeManager;
 
    public function __construct(
 	......
        \Magento\Store\Model\StoreManagerInterface $storeManager
	 .......
    ) {
	 .....
        $this->_storeManager = $storeManager;
 	.....
    }
 
    public function myStoreBaseUrls(){
 	$storeManager = $this->_storeManager;
 	$statiContenteUrl = $storeManager->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_STATIC);
	$mediaUrl = $storeManager->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);
 	$linkUrl = $storeManager->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_LINK);
 	$baseUrlwithoutIndexPhp = $storeManager->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_WEB);
  	return $this->_storeManager->getStore()->getBaseUrl();
     }
    public function myStoreUrl(){
 	return $this->_storeManager->getStore()->getBaseUrl()
    }

Want to get  Base Url with Out Index.php then you below code:

$baseUrlWithOutIndexPhp = $storeManager->getStore()
->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_WEB);

The above code always gives the URL without Index.php either Seo rewrite enabled at the admin or not.It does not depend on that setting.

Where,

$linkUrl= $storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_LINK);

It gives Base URL with index.php http://example.com/index.php/  when SEO rewrite is not enabled.

And give Base URL without index.php http://example.com/index.php/  when SEO rewrite is enabled.


No comments:

Post a Comment

  How to Upgrade magento version from 2.3.x [EE] to 2.3.x [EE] Here are the steps I used to successfully update Magento 2.3.3 [EE] to 2.3.5 ...