Monday, 16 March 2015

How to get current Page URL and Identifiers in Magento

How to get current Page URL and Identifiers in Magento


There are times when you want to perform some event on particular page.
In Magento to detect the current page, following code can be used.
1) To Get the name of the current router use following code :
Mage::app()->getFrontController()->getRequest()->getRouteName();
There are majorly four routes
a) CMS : It includes all CMS pages
b) Catalog : It includes listing and detail page
c) Checkout : It includes shopping basket, checkout and success page
d) Customer : It includes all customer pages
CMS page : To get the identifier of the current CMS page you can use
Mage::getSingleton(‘cms/page’)->getIdentifier();
To get the Page Title :
Mage::getSingleton(‘cms/page’)->getTitle();
To get the Page ID :
Mage::getBlockSingleton(‘cms/page’)->getPage()->getId();
Category Page : To get the category name in product listing page use
Mage::registry(‘current_category’)->getName();
This code will throw an error on other pages than catalog. So please check the router to be catalog before executing this code.
Also, In the last case you can always use the URL to find on which page you are
Please see my post on URL’s to check How To Get Current URL
To call a phtml template file in another phtml file you can use following code.
<?php echo $this->getLayout()->createBlock(‘core/template’)->setTemplate(‘templateFolder/yourtemplate.phtml’)->toHtml(); ?>


No comments:

Post a Comment