Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get current module and page identifier in Magento?

Tags:

magento

I need to get Current page Identifier and current module in Magento.

I used the below code.

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

Its working once clear the magento cache otherwise it shows the old page and module.

Example:

When we check in home page it gives 'cms' as module and 'home' as page. Now I click the Contact page now also it shows same result.

After clear the cache and check the contact page it shows "cms" as modle and 'contact' as page Identifier.

How to get current page Identifier and module without clear cache every time?

like image 510
Sankar Subburaj Avatar asked Apr 30 '12 11:04

Sankar Subburaj


4 Answers

To get current module:

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

To get current CMS page:

Mage::getSingleton('cms/page')->getIdentifier()
like image 72
Sankar Subburaj Avatar answered Nov 13 '22 19:11

Sankar Subburaj


 $pageID = Mage::getBlockSingleton('cms/page')->getPage()->getIdentifier(); 

this will return current cms page identifier.

like image 29
Sumith Harshan Avatar answered Nov 13 '22 19:11

Sumith Harshan


To get correct identifier and URL of 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 will help someone.

like image 29
Sandesh Avatar answered Nov 13 '22 18:11

Sandesh


Most likely you'll need to overwrite the constructor to the block you're wanting, 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),
        ));
    }

Should just be:

protected function _construct() {}
like image 32
B00MER Avatar answered Nov 13 '22 18:11

B00MER