Magento CE 1.7.0.2 - 仅在footer.phtml上启用缓存时才会出现问题

I'm experiencing this problem with Magento when caching is enabled...

Problem:

Using the following in footer I can get the current CMS page title:

$this->getLayout()->getBlock('head')->getTitle();

^^ I have used this and other methods getting current URL in footer and experience same symptoms. With caching off it gets the current page no problem but with caching on it will get for example the page title for the shop then visiting other page shows that it is still shop.

Here is a problem scenario with caching enabled:

  1. Visit homepage (http://devtest.eboost.com/).

  2. Navigate to blog and scroll to the bottom so you can see footer. The hyperlinks for Powders, Shots, and some from Company should be different on homepage and all other pages. It behaves just fine with caching off. The links work fine in header.phtml and has the same logic.

Thing is with caching enabled it doesn't behave like this when in the header.phtml file so I'm confused. Do I need to do something special or maybe somehow take footer.phtml out of cache monitoring? I don't think removing it from cache monitoring would be wise because we have most of our JavaScript in this document.

I'm not sure what to do and very pressured to figure this out to make our client's website live.

Update: @benmarks 's Answer is miles better. Use that.

Try removing the footer from the cache. Try this - http://ken.edmonds-commerce.co.uk/magento/disable-footer-block-caching-in-magento/

The application assumes that the footer block is not dynamic and it is therefore cached permanently. While this is a questionable decision for the core team to have made, you have two options: disable the cache or change the cache key to incorporate the dynamic parameters. Both can be done via class rewrite or via layout XML:

block_html cache has three "settings":

  • null = never cache
  • false = cache forever
  • int = cache for this many seconds

In its constructor, Mage_Page_Block_Html_Footer sets its cache_lifetime property to false - hence its "permanently cached" behavior. Because this property is publicly accessible via the magic setCacheLifetime() method, it is possible to set its value from layout XML. Unfortunately there is no way to pass null as a value from layout XML as a string, however, it is possible to pass values via helpers. Therefore a helper method which returns null should work. There are some core helpers which do this; an example usage would be as follows (could be placed in your custom theme's local.xml which you may need to create):

<?xml version="1.0" encoding="UTF-8"?>
<layout>
    <default>
        <action method="setCacheLifetime" block="footer">
            <value helper="usa/convertMeasureWeight">
                <x>0</x>
                <x>0</x>
                <x>0</x>
            </value>
            <!--
                i.e.
                Mage_Page_Block_Html_Footer->setCacheLifetime(
                    Mage_Usa_Helper_Data::convertMeasureWeight(0,0,0)
                );
            -->
        </action>
    </default>
</layout>

That should work. However, this feels a bit hackish, so you could create your own helper class and just simply define a method to return null for the explicit purpose of setting this value.