How to Get Magento 2 Base URL & Current URL in phtml
Vinh Jacker | 12-18-2024
Magento 2, one of the most robust eCommerce platforms, provides a variety of ways to retrieve vital information for development. Two commonly requested pieces of data during Magento 2 development are the base URL and the current URL. Whether you’re working on themes, modules, or custom integrations, understanding how to access these URLs efficiently can streamline your development process and enhance the user experience.
In this article, we’ll guide you step by step on how to get the base URL and current URL in Magento 2.
3 Steps to Get Base URL and Current URL in Magento 2
- Step 1: Declare in
Mageplaza_HelloWorld
- Step 2: Get current URL and base URL in the template (.phtml) file
- Step 3: Flush Cache and check result
Step 1: Declare in Mageplaza_HelloWorld
You will use a block class of the module Mageplaza_HelloWorld
, then possibly inject the object of StoreManagerInterface
and UrlInterface
in the constructor of the module’s block class. You will work with two functions in the below class: getStoreManagerData()
and getUrlInterfaceData()
.
-
In
getStoreManagerData()
function, you will use object ofStoreManagerInterface
to get the base and current URL. -
In
getUrlInterfaceData()
function, you will use object ofUrlInterface
to get the base and current URL.
Open app/code/Mageplaza/HelloWorld/Block/HelloWorld.php
class and run the code:
<?php
namespace Mageplaza\HelloWorld\Block;
class HelloWorld extends \Magento\Framework\View\Element\Template
{
protected $_storeManager;
protected $_urlInterface;
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);
}
public function _prepareLayout()
{
return parent::_prepareLayout();
}
/**
* Prining URLs using StoreManagerInterface
*/
public function getStoreManagerData()
{
echo $this->_storeManager->getStore()->getId() . '<br />';
// by default: URL_TYPE_LINK is returned
echo $this->_storeManager->getStore()->getBaseUrl() . '<br />';
echo $this->_storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_WEB) . '<br />';
echo $this->_storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_DIRECT_LINK) . '<br />';
echo $this->_storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA) . '<br />';
echo $this->_storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_STATIC) . '<br />';
echo $this->_storeManager->getStore()->getUrl('product/33') . '<br />';
echo $this->_storeManager->getStore()->getCurrentUrl(false) . '<br />';
echo $this->_storeManager->getStore()->getBaseMediaDir() . '<br />';
echo $this->_storeManager->getStore()->getBaseStaticDir() . '<br />';
}
/**
* Prining URLs using URLInterface
*/
public function getUrlInterfaceData()
{
echo $this->_urlInterface->getCurrentUrl() . '<br />';
echo $this->_urlInterface->getUrl() . '<br />';
echo $this->_urlInterface->getUrl('helloworld/general/enabled') . '<br />';
echo $this->_urlInterface->getBaseUrl() . '<br />';
}
}
?>
See more functions in
– MAGENTO_ROOT/vendor/magento/module-store/Model/Store.php – MAGENTO_ROOT/vendor/magento/framework/Url.php
Step 2: Get base URL and current URL in the template file
Please use the following code:
<?php echo $block->getUrl('hello/test'); ?>
<?php echo $block->getBaseUrl(); ?>
Step 3: Flush Cache and check result
Flush Magento cache and check your result.
Conclusion
That’s all. Getting based and current URLs simplifies the work for store admins as they do not need to find and type in the full URL when building a page or attaching the URLs somewhere. Hope that you will find this article useful for your project.
Read also: How to Configure Store URLs in Magento 2
Related Post