Getting the last url and determine if it’s a home page or any other pages in Magento
Sometimes it’s useful to know the last url and determine if it is a home page, a category page, a product detail page or any other pages.Here is a function that demonstrates how to get the last url and determine if it is a home page, a category page or a product detail page. This function can be placed in any classes in Magento, feel free to change the return type from string to boolean or anything else that suits your need.
<?php public function getLastPage() { $lastUrl = Mage::getSingleton('core/session')->getLastUrl(); if (preg_match("#cms/index/index#", $lastUrl)) { return "Last page was:<br>Home page<br/>url: ".$lastUrl; } elseif(preg_match("#catalog/category/view#", $lastUrl)) { return "Last page was:<br>Category page<br/>url: ".$lastUrl; } elseif (preg_match("#catalog/product/view#", $lastUrl)) { return "Last page was:<br>Product detail page<br/>url: ".$lastUrl; } else { return "Last page was:<br>Unknown page<br/>url: ".$lastUrl; } } ?>
Here are some quick notes of how to get other urls in Magento’s phtml files or php class files:
Current url
<?php Mage::helper('core/url')->getCurrentUrl(); ?>
Controller route name
<?php Mage::app()->getFrontController()->getRequest()->getRouteName(); ?>
Base url
<?php Mage::getBaseUrl(); ?>
Home url
<?php Mage::helper('core/url')->getHomeUrl(); ?>
Store url
<?php Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB); ?>
Media url
<?php Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA); ?>
Search within Codexpedia
Custom Search
Search the entire web
Custom Search
Related Posts