How to check if current url is homepage in Magento 2

This post gives you a simple way to confirm if the current page you are looking at is the homepage or not by using the PHP code.

How to check if the current URL is the homepage URL? You are also wondering that question and also finding the perfect way for your Magento 2 store. If you have ever been not sure if a page is a homepage or not, this post will guide you to clarify it by using the PHP code.

Let’s start!

3 Steps to check if current url is homepage 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 Logo class in the constructor of the module’s block class.

app/code/Mageplaza/HelloWorld/Block/HelloWorld.php

<?php
namespace Mageplaza\HelloWorld\Block;
class HelloWorld extends \Magento\Framework\View\Element\Template
{
	protected $_logo;	
	
	public function __construct(
		\Magento\Backend\Block\Template\Context $context,
		\Magento\Theme\Block\Html\Header\Logo $logo,
		array $data = []
	)
	{		
		$this->_logo = $logo;
		parent::__construct($context, $data);
	}
	
	/**
     * Check if current url is url for home page
     *
     * @return bool
     */
    public function isHomePage()
    {	
		return $this->_logo->isHomePage();
	}
}
?>

You can see more functions in vendor/magento/module-theme/Block/Html/Header/Logo.php.

Step 2: Declare function in template .phtml file

Run the below function in your template .phtml file

if ($block->isHomePage()) {
    // do something
}

Step 3: Get output in index.php file

To get the output of the bottom of the current page (homepage or another page), you can write the following script in the index.php file.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$helloWorldBlock = $objectManager->get('Mageplaza\HelloWorld\Block\HelloWorld');
var_dump($helloWorldBlock->isHomePage());

Please follow three steps and you can check the current url. In case that you have any queries about the article or any questions in general, use the comment section below!

Related Post

Wrap up

You have got a simple and effective solution to confirm if the current page is a homepage or not. Please follow three steps and you can check the current url. In case that you have any queries about the article or any questions in general, use the comment section below!

Enjoyed the tutorial? Spread it to your friends!

magento-2-tutorial
check
current
url
homepage

Sam Thomas
Sam Thomas

CEO and Founder of Mageplaza. Pursueing a simple and healthy lifestyle. A friend, a husband and a dad of two children, a trainer and an influencer wannabe. He is a big fan of sports and travel, also.

People also searched for

  • magento 2 check if current url homepage
  • check if current url homepage in magento 2
  • how to check if current url homepage
  • how to check if current url homepage in magento 2
  • magento 2 check current page
  • magento 2 check if homepage
  • magento check if homepage
  • magento 2 get current page identifier
  • magento 2 check if current page is category page
  • magento 2 check if product page
  • magento 2 get current page
  • magento 2 get home page url
  • magento 2 get page url
  • 2.2.x, 2.3.x, 2.4.x