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(); ?>

Advertisements

3 thoughts on “How to get current Page URL and Identifiers in Magento

  1. How to get magento homepage

    We use

    $route = Mage::app()->getFrontController()->getRequest()->getRouteName();

    $action = Mage::app()->getFrontController()->getRequest()->getActionName();

    if($route == ‘cms’ && $action == ‘index’):

    echo $this->getChildHtml(‘shopper_footer_partners’);

    endif;

    Like

    • To get the home page

      1) Note the url key of the Home Page for example: ‘home’
      2) add the following code in the Template file ie: 1column, 2column-left etc

      $page = Mage::app()->getFrontController()->getRequest()->getRouteName();

      if($page == ‘cms’){
      if(Mage::getSingleton(‘cms/page’)->getIdentifier()==’home’){
      // ADD YOUR ACTION OVER HERE
      }
      }

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s