X
    Categories: Knowledgebase

Magento : Get Base Url , Skin Url , Media Url , Js Url , Store Url and Current Url

MAGENTO 1 :

Get Url in phtml files

<!-- Get Base Url -->
Mage::getBaseUrl();

<!-- Get Skin Url -->
Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_SKIN);
<!-- Unsecure Skin Url -->
$this->getSkinUrl('images/imagename.jpg');
<!-- Secure Skin Url -->
$this->getSkinUrl('images/imagename.gif', array('_secure'=>true));

<!-- Get Media Url -->
Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA);

<!-- Get Js Url -->
Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_JS);

<!-- Get Store Url -->
Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);

<!-- Get Current Url -->
Mage::helper('core/url')->getCurrentUrl();

Get Url in cms pages or static blocks

<!-- Get Base Url -->
{{store url=""}}

<!-- Get Skin Url -->
{{skin url='images/imagename.jpg'}}

<!-- Get Media Url -->
{{media url='/imagename.jpg'}}

<!-- Get Store Url -->
{{store url='mypage.html'}}

MAGENTO 2 :

Below is a block class of my custom module. I have injected object of StoreManagerInterface & UrlInterface in the constructor of my module’s block class. Both of them can be used to fetch base and current URL. In below class, in function getStoreManagerData(), object of StoreManagerInterface is used to print the base and current url and in function getUrlInterfaceData() function, object of UrlInterface is used to print the base and current url.

Type variable :

protected $_storeManager;
protected $_urlInterface;

With __construct :

public function __construct(
    MagentoBackendBlockTemplateContext $context,        
    MagentoStoreModelStoreManagerInterface $storeManager,
    MagentoFrameworkUrlInterface $urlInterface,    
    array $data = []
)
{        
    $this->_storeManager = $storeManager;
    $this->_urlInterface = $urlInterface;
    parent::__construct($context, $data);
}

Then you can Prining URLs using StoreManagerInterface

/**
 * Prining URLs using StoreManagerInterface
 */
public function getStoreManagerData()
{    
    // get BaseUrl
    echo $this->_storeManager->getStore()->getBaseUrl();   
    // get URL_TYPE_WEB     
    echo $this->_storeManager->getStore()->getBaseUrl(MagentoFrameworkUrlInterface::URL_TYPE_WEB);
    // get URL_TYPE_DIRECT_LINK   
    echo $this->_storeManager->getStore()->getBaseUrl(MagentoFrameworkUrlInterface::URL_TYPE_DIRECT_LINK);
    // get URL_TYPE_MEDIA   
    echo $this->_storeManager->getStore()->getBaseUrl(MagentoFrameworkUrlInterface::URL_TYPE_MEDIA);
    // get URL_TYPE_STATIC   
    echo $this->_storeManager->getStore()->getBaseUrl(MagentoFrameworkUrlInterface::URL_TYPE_STATIC);
    // get Url  
    echo $this->_storeManager->getStore()->getUrl('product/33');
    // get CurrentUrl  
    echo $this->_storeManager->getStore()->getCurrentUrl(false);
    // get BaseMediaDir 
    echo $this->_storeManager->getStore()->getBaseMediaDir();
    // get BaseStaticDir
    echo $this->_storeManager->getStore()->getBaseStaticDir();  
}

And you can Prining URLs using URLInterface

/**
 * Prining URLs using URLInterface
 */
public function getUrlInterfaceData()
{
    // get CurrentUrl 
    echo $this->_urlInterface->getCurrentUrl();
    // get Url 
    echo $this->_urlInterface->getUrl();
    // get Url with value
    echo $this->_urlInterface->getUrl('test/data/22');
    // get BaseUrl 
    echo $this->_urlInterface->getBaseUrl();
}

As you can see in the above code, my block class is extending class MagentoFrameworkViewElementTemplate. Hence, I can easily get URL and base URL in my template (.phtml) file with the following code:

<?php echo $block->getUrl('hello/test'); ?>
<?php echo $block->getBaseUrl(); ?>

Or Using Object Manager

$objectManager =  MagentoFrameworkAppObjectManager::getInstance();        
$appState = $objectManager->get('MagentoFrameworkAppState');
$appState->setAreaCode('frontend');
$storeManager = $objectManager->get('MagentoStoreModelStoreManagerInterface');
$store = $storeManager->getStore();

// get Url with value
echo $store->getUrl('product/33');
// get CurrentUrl
echo $store->getCurrentUrl();
// get BaseUrl
echo $store->getBaseUrl();
// get URL_TYPE_WEB
echo $store->getBaseUrl(MagentoFrameworkUrlInterface::URL_TYPE_WEB);
// get URL_TYPE_MEDIA
echo $store->getBaseUrl(MagentoFrameworkUrlInterface::URL_TYPE_MEDIA);

Thank for reading this post, Hope it helps.

Kien Wiliam: Magento Ecommerce Developer