Home » Magento, Magento 212 May 2016

Magento 2: Check if Current URL is Homepage

This article shows how we can check if the current page URL is homepage URL in Magento 2.

Below is a block class of my custom module (Chapagain_HelloWorld). I have injected object of Logo block class in the constructor of my module’s block class.

app/code/Chapagain/HelloWorld/Block/HelloWorld.php

See more functions in vendor/magento/module-theme/Block/Html/Header/Logo.php.

Now, we use can the function in our template (.phtml) file.

You can write the following code at the bottom of your site’s index.php file and see the output at the bottom of your page when you are in homepage or in any other page:

Hope this helps. Thanks.

Magento 2

Get New Post by Email

Find me on

FacebookTwitterGoogle+LinkedInRSS Feed
  • magento

  • Kusangi Thakkar

    Hello,
    Great Post.
    But I have a doubt to ask
    If I am not making my own controller,just the Block and View files to load paralely with Magento store
    Do I have to do any additional configurations?
    This is my layout file code:

  • It should work fine. You may also refer to this tutorial on creating a simple helloworld module in magento 2 http://blog.chapagain.com.np/magento-2-create-a-simple-helloworld-module-step-by-step-beginner-tutorial/

  • adel

    Hello i am trying to display a in 1 column but i only want to show it on home page
    my code is this :
    $currentUrl = Mage::helper(‘core/url’)->getCurrentUrl();
    $homepageurl=Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);
    if(“$currentUrl” == “$homepageurl”)

    my if condition is giving error