9

I want check current page is home page, category page, product page & cms page in magento 2

  • in what context do you want to check this? controller? block? – Marius Dec 9 '15 at 11:02
  • Magento 1 version we use function Mage::getBlockSingleton('page/html_header')->getIsHomePage(); i want to show data pagewise like homepage,category page,product page & cms page etc – MagikVishal Dec 9 '15 at 11:08
  • 1
    I know that, but in order to answer this I want to know where do you want to use this in magento2. There is not global mage class in m2. – Marius Dec 9 '15 at 11:09
20

You can try this: Inject an instance of \Magento\Framework\App\Request\Http in your class constructor. If you are in a controller you don't need to do it. You can already access it like this $request = $this->getRequest()

public function __construct(
    ...
    \Magento\Framework\App\Request\Http $request
) {
    ...
    $this->_request = $request;
}

Then you can check if is homepage like this:

if ($this->_request->getFullActionName() == 'cms_index_index') {
    //you are on the homepage
}
if ($this->_request->getFullActionName() == 'catalog_product_view') {
    //you are on the product page
}
if ($this->_request->getFullActionName() == 'catalog_category_view') {
    //you are on the category page
}
  • @marius - how do i check the same in a phtml file ? – Manashvi Birla Apr 5 '16 at 8:36
  • 2
    write a method in the block that returns $this->_request->getFullActionName() ad use it in the phtml file. – Marius Apr 5 '16 at 9:14
  • Could you elaborate? How does one do that? – Max Oct 5 '16 at 22:31
  • @Max what more elaboration do you need? I think the answer is clear? tell me what you want to know – Marius Oct 6 '16 at 6:19
  • Sorry, I re-read it and its clear. Thank you. – Max Oct 6 '16 at 7:39
6

From inside phtml file this worked for me:

if ($this->getRequest()->getFullActionName() == 'cms_index_index') {
    //you are on the homepage
}
if ($this->getRequest()->getFullActionName() == 'catalog_product_view') {
    //you are on the product page
}
if ($this->getRequest()->getFullActionName() == 'catalog_category_view') {
    //you are on the category page
}
  • it works for me – sandip Sep 14 '17 at 11:24
3

Try this one:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$request = $objectManager->get('Magento\Framework\App\Action\Context')->getRequest();
if ($request->getFullActionName() == 'cms_index_index') {
    // is homepage
}
  • 2
    don't use object manager – Marius Dec 9 '15 at 11:35
  • This worked for me. Why not use Object Manager? – TheBlackBenzKid Feb 15 '17 at 10:59
  • It's bad practice to use the Object Manager directly. Magento Official dev doc says "Magento prohibits the direct use of the ObjectManager in your code because it hides the real dependencies of a class." – Makwana Ketan Jul 20 '17 at 10:33
0

Because of the design pattern Dependency Injection. You create a module to request a resource on demand. Object manager is going against that paradigm. However, it works great but its like using Mage all over again--slow.

  • whoops supposed to comment above, my b. – Chris Anderson Jul 4 '17 at 22:35
0

Try below code:

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);
}

public function isHomePage()
{   
    return $this->_logo->isHomePage();
}

Using Object Manager

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$logo = $objectManager->get('Magento\Theme\Block\Html\Header\Logo');
var_dump($logo->isHomePage());

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.