Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Problems getting Current URL on template
 
lucianoserfe
Jr. Member
 
Total Posts:  3
Joined:  2012-03-16
 

This is my problem

I am actually at my navigation top template that is in:

design/frontend/default/mytheme/template/catalog/navigation/top.phtml

And what I want to do is to restrict the menu tabs shows depending on the actual URL.
This is an example of the code:

<?php
    $_menu 
'';
    
Mage::getSingleton('core/session')->setshowProductListSF('true');
    foreach (
$this->getStoreCategories() as $_category{
        $_menu 
.= $this->drawItem($_category);
    
}
    Mage
::getSingleton('core/session')->setshowProductListSF();
?>

<?php 
if ($_menu): ?>
<div class="nav-container">
    <
ul id="nav">
        
<?php if (isset($_GET["frommyaccount"]) && ($_GET["frommyaccount"== 'true')): ?>
            <?php 
echo $this->getLayout()->createBlock('cms/block')->setBlockId('menu-validate-giftcard')->toHtml() ?>
        <?php 
else: ?>
            
<li class="level0 first nav-home"><a href="<?php echo $this->getUrl() ?>"><span><?php echo $this->__('Home'?></span></a></li>
            
<?php if (strpos($_SERVER["REQUEST_URI"]'/customer') === false): ?>
                <?php 
echo $this->getLayout()->createBlock('cms/block')->setBlockId('menu-before-products')->toHtml() ?>
                <?php 
echo $_menu?>
                <?php 
echo $this->getLayout()->createBlock('cms/block')->setBlockId('menu-after-products')->toHtml() ?>
            <?php 
else: ?>
                <?php 
echo $this->getLayout()->createBlock('cms/block')->setBlockId('menu-after-products-customer')->toHtml() ?>
            <?php 
endif; ?>
        <?php 
endif; ?>
    
</ul>
</
div>
<?php endif; ?>

The problem is that magento is giving to me the same variable all the time when I check $_SERVER["REQUEST_URI"] and dont know why.
I have tried also with

$this->helper('core/url')->getCurrentUrl();

and also give it to me the same url on every page.
For example, If I was on “/customer/account” and clean the cache HTML and reload page, then the Current URL where always “/customer/account”

Any idea about this?

Regards,
Luciano

 
Magento Community Magento Community
Magento Community
Magento Community
 
muk_t
Sr. Member
 
Total Posts:  226
Joined:  2012-07-24
 

Hi,

I am also facing the same problem

$this->helper('core/url')->getCurrentUrl();
gives the cached URL.If you are using Magento Enterprise edition then try to disable the Blocks HTML output cache.

If then you get every thing correct.It is confirmed that it is because of caching.

I also don’t know the solution
Thanks

 
Magento Community Magento Community
Magento Community
Magento Community
 
tzyganu
Mentor
 
Avatar
Total Posts:  2205
Joined:  2009-11-18
Bucharest, Romania
 

Hello
@muk_t: The problem is not actually a problem. It’s because of the blocks cache.
You can try to include in the cache key of your block the url of the page so for different url’s you will get a different cache.

For example if you override the Mage_Catalog_Block_Navigation you need to make the getCacheKeyInfo method look like this:

public function getCacheKeyInfo()
    
{
        $shortCacheId 
= array(
            
'CATALOG_NAVIGATION',
            
Mage::app()->getStore()->getId(),
            
Mage::getDesign()->getPackageName(),
            
Mage::getDesign()->getTheme('template'),
            
Mage::getSingleton('customer/session')->getCustomerGroupId(),
            
'template' => $this->getTemplate(),
            
'name' => $this->getNameInLayout(),
            
$this->getCurrenCategoryKey(),
            
Mage::app()->getFrontController()->getAction()->getFullActionName()//I added this line
        
);
        
$cacheId $shortCacheId;

        
$shortCacheId array_values($shortCacheId);
        
$shortCacheId implode('|'$shortCacheId);
        
$shortCacheId md5($shortCacheId);

        
$cacheId['category_path'$this->getCurrenCategoryKey();
        
$cacheId['short_cache_id'$shortCacheId;

        return 
$cacheId;
    
}

You can even optimize it.
For example in the case of lucianoserfe he wanted a different menu only in pages related to customers
In this case the code above becomes:

public function getCacheKeyInfo()
    
{
        $shortCacheId 
= array(
            
'CATALOG_NAVIGATION',
            
Mage::app()->getStore()->getId(),
            
Mage::getDesign()->getPackageName(),
            
Mage::getDesign()->getTheme('template'),
            
Mage::getSingleton('customer/session')->getCustomerGroupId(),
            
'template' => $this->getTemplate(),
            
'name' => $this->getNameInLayout(),
            
$this->getCurrenCategoryKey(),
            (int)(
strpos(Mage::app()->getFrontController()->getAction()->getFullActionName(), 'customer_') !== false)
        );
        
$cacheId $shortCacheId;

        
$shortCacheId array_values($shortCacheId);
        
$shortCacheId implode('|'$shortCacheId);
        
$shortCacheId md5($shortCacheId);

        
$cacheId['category_path'$this->getCurrenCategoryKey();
        
$cacheId['short_cache_id'$shortCacheId;

        return 
$cacheId;
    
}
The line I added in the cache key:
Mage::app()->getFrontController()->getAction()->getFullActionName()
returns an alias for the page. It’s the same used in the layout files for identifying a page:
Here are some possible values:
cms_index_index - for homepage
catalog_category_view - for a category page
catalog_product_view - for a product page
customer_account_login - login page
customer_account_index - customer account dashboard.
and so on.

Hope it helps.
Marius.

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top