Magento current page url Methods.How Can we get the URL in amgento category Page / Product/CMS Page
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
| <?php $current_page = '' ; /* * Check to see if its a CMS page * if it is then get the page identifier */ if (Mage::app()->getFrontController()->getRequest()->getRouteName() == 'cms' ): $current_page = Mage::getSingleton( 'cms/page' )->getIdentifier(); endif ; /* * If its not CMS page, then just get the route name */ if ( empty ( $current_page )): $current_page = Mage::app()->getFrontController()->getRequest()->getRouteName(); endif ; /* * What if its a catalog page? * Then we can get the category path <img src="http://www.justwebdevelopment.com/blog/wp-includes/images/smilies/icon_smile.gif" alt=":)" class="wp-smiley"> */ /* * Or you can check all values * $current_page_array = Mage::registry('current_category'); * $current_page_array['url_path'] */ if ( $current_page == 'catalog' ): $current_page = 'path-' . preg_replace( '#[^a-z0-9]+#' , '-' , strtolower (Mage::registry( 'current_category' )->getUrlPath())); endif ; ?> |
Also if you want Full Current Page URL in Magento you can do so with this one line.
1
2
3
| <?php $currentUrl = $this ->helper( 'core/url' )->getCurrentUrl(); ?> |
Another way to get current url
1
2
3
4
5
6
7
8
9
10
| <?php $urlRequest = Mage::app()->getFrontController()->getRequest(); $urlPart = $urlRequest ->getServer( 'ORIG_PATH_INFO' ); if ( is_null ( $urlPart )) { $urlPart = $urlRequest ->getServer( 'PATH_INFO' ); } $urlPart = substr ( $urlPart , 1 ); $currentUrl = $this ->getUrl( $urlPart ); ?> |
Also you can check current page or catalog page is product page or not with “Mage::registry”.
1
2
3
4
5
6
| <?php $onCatalogFlag = false; if (Mage::registry( 'current_product' )) { $onCatalogFlag = true; } ?> |
And also try to something like below code
1
2
3
4
5
| <?php echo $this ->getRequest()->getControllerName(); if ( $this ->getRequest()->getControllerName()== 'product' ) //do something if ( $this ->getRequest()->getControllerName()== 'category' ) //do others ?>
++++++++++++++++++++++++++++++++=
Method 2
|
1 comments:
$currentUrl = Mage::helper(‘core/url’)->getCurrentUrl()
or
$currentUrl = Mage::getUrl(‘*/*/*’, array(‘_current’ => true));
above code may not work always as expected.
Better way to find the current url is to use the following code:
if (!in_array(Mage::app()->getFrontController()->getAction()->getFullActionName(), array(‘cms_index_noRoute’, ‘cms_index_defaultNoRoute’))) {
$currentUrl = Mage::helper(‘core/url’)->getCurrentUrl();
}
Source: http://www.blog.magepsycho.com/how-to-find-current-url-in-magento/
Post a Comment