Cookies setting

Cookies help us enhance your experience on our site by storing information about your preferences and interactions. You can customize your cookie settings by choosing which cookies to allow. Please note that disabling certain cookies might impact the functionality and features of our services, such as personalized content and suggestions. Cookie Policy

Cookie Policy
Essential cookies

These cookies are strictly necessary for the site to work and may not be disabled.

Information
Always enabled
Advertising cookies

Advertising cookies deliver ads relevant to your interests, limit ad frequency, and measure ad effectiveness.

Information
Analytics cookies

Analytics cookies collect information and report website usage statistics without personally identifying individual visitors to Google.

Information
mageplaza.com

How to Get Magento 2 Base URL & Current URL in phtml

Vinh Jacker | 12-18-2024

How to get Magento 2 base URL, Current URL in phtml

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

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 of StoreManagerInterface to get the base and current URL.

  • In getUrlInterfaceData() function, you will use object of UrlInterface 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

x
    Jacker

    With over a decade of experience crafting innovative tech solutions for ecommerce businesses built on Magento, Jacker is the mastermind behind our secure and well-functioned extensions. With his expertise in building user-friendly interfaces and robust back-end systems, Mageplaza was able to deliver exceptional Magento solutions and services for over 122K+ customers around the world.



    Related Post

    Website Support
    & Maintenance Services

    Make sure your store is not only in good shape but also thriving with a professional team yet at an affordable price.

    Get Started
    mageplaza services