Get current module and page id in Magento?

I need to get the current page id and current module in Magento.

I used the code below.

Mage::app()->getRequest()->getModuleName() - To get current module. Mage::getSingleton('cms/page')->getIdentifier() - To get current page 

As a result, he will clear the magento cache, otherwise he will show the old page and module.

Example:

When we register on the home page, it gives "cms" as a module and "home" as a page. Now I click on the page "Contacts" and now it shows the same result.

After clearing the cache and checking the contact page, it displays "cms" as modle and "contact" as the page identifier.

How to get the current page id and module without a transparent cache every time?

+8
magento
source share
4 answers

To get the current module:

 Mage::app()->getFrontController()->getRequest()->getModuleName() 

To get the current CMS page:

 Mage::getSingleton('cms/page')->getIdentifier() 
+12
source share
  $pageID = Mage::getBlockSingleton('cms/page')->getPage()->getIdentifier(); 

this will return the current cms page id.

+4
source share

To get the correct id and URL of the current page:

 if (!in_array(Mage::app()->getFrontController()->getAction()->getFullActionName(), array('cms_index_noRoute', 'cms_index_defaultNoRoute'))) { $Identifier = Mage::app()->getFrontController()->getRequest()->getModuleName(); $currentUrl = Mage::helper('core/url')->getCurrentUrl(); } $Identifier = strtolower($Identifier); if($Identifier == 'cms'){ $Identifier = Mage::getSingleton('cms/page')->getIdentifier(); } echo $Identifier = strtolower($Identifier).'<br>'.$currentUrl; 

This is what works for me in many cases. Hope this helps someone.

+3
source share

Most likely, you will need to rewrite the constructor in the desired block, for example: Mage_Catalog_Block_Navigation

So your _constrct ()

 protected function _construct() { $this->addData(array( 'cache_lifetime' => false, 'cache_tags' => array(Mage_Catalog_Model_Category::CACHE_TAG, Mage_Core_Model_Store_Group::CACHE_TAG), )); } 

It should only be:

 protected function _construct() {} 
+2
source share

All Articles