MAGENTO 1 :
Get Url in phtml files
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<!-- 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
1 2 3 4 5 6 7 8 9 10 11 |
<!-- 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 :
1 2 |
protected $_storeManager; protected $_urlInterface; |
With __construct :
1 2 3 4 5 6 7 8 9 10 11 |
public function __construct( \Magento\Backend\Block\Template\Context $context, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Framework\UrlInterface $urlInterface, array $data = [] ) { $this->_storeManager = $storeManager; $this->_urlInterface = $urlInterface; parent::__construct($context, $data); } |
Then you can Prining URLs using StoreManagerInterface
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
/** * Prining URLs using StoreManagerInterface */ public function getStoreManagerData() { // get BaseUrl echo $this->_storeManager->getStore()->getBaseUrl(); // get URL_TYPE_WEB echo $this->_storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_WEB); // get URL_TYPE_DIRECT_LINK echo $this->_storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_DIRECT_LINK); // get URL_TYPE_MEDIA echo $this->_storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA); // get URL_TYPE_STATIC echo $this->_storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
/** * 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:
1 2 |
<?php echo $block->getUrl('hello/test'); ?> <?php echo $block->getBaseUrl(); ?> |
Or Using Object Manager
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $appState = $objectManager->get('\Magento\Framework\App\State'); $appState->setAreaCode('frontend'); $storeManager = $objectManager->get('\Magento\Store\Model\StoreManagerInterface'); $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(\Magento\Framework\UrlInterface::URL_TYPE_WEB); // get URL_TYPE_MEDIA echo $store->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA); |
Thank for reading this post, Hope it helps.