php - 在启用 BLOCK_HTML 缓存的情况下检测 Magento .phtml 中的主页

标签 php magento magento-1.4

我在 catalog/navigation/vert_nav.phtml 中尝试了以下两种方法来添加或抑制特定于主页的内容:

if($this->getUrl('') == $this->getUrl('*/*/*', array('_current'=>true, '_use_rewrite'=>true))):

if(
Mage::getSingleton('cms/page')->getIdentifier() == 'home'  &&
Mage::app()->getFrontController()->getRequest()->getRouteName() == 'cms' 
) :

两者都工作正常,但是当 BLOCK_HTML 缓存打开时,它首先工作,然后在一段时间后主页开始显示仅供其他页面使用的内容(在我使用 lower down 的 else 子句之后)。当我关闭 BLOCK_HTML 时,它的行为符合预期。

有趣的是,我在 page/html/head.phtml(针对主页特定的 javascript/css)和 page/html/header.phtml(对于标题横幅应该仅出现在主页上),即使 BLOCK_HTML 处于开启状态,这些也能正常工作。

(Magento 1.4.1.1)

最佳答案

上面的答案是最好的解决方案。

你可以简单地复制 app/code/core/Mage/Catalog/Block/Nagivation.php

到:

app/code/local/Mage/Catalog/Block/Nagivation.php

然后如上所述更改 getCacheKeyInfo() 方法。

/**
 * Get Key pieces for caching block content
 *
 * @return array
 */
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(),
        // Your logic to make home/none home have different cache keys
        Mage::getSingleton('cms/page')->getIdentifier() == 'home' ? '1' : '0'
    );
    $cacheId = $shortCacheId;

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

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

    return $cacheId;
}

这将使主页/非主页页面的缓存键不同,这将缓存两个副本,而不是缓存单个模板副本以供所有页面使用。

关于php - 在启用 BLOCK_HTML 缓存的情况下检测 Magento .phtml 中的主页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12403675/

相关文章:

magento - 获取产品的父类别,即使它是直接访问的

php - Magento - 从数据库手动将订单从 1.4 迁移到 1.7

magento - 从外部管理面板链接到 Magento Admin

php - PhalconPHP : eager load related records?

css - Magento 绝对定位主屏幕 CSS 设置

带有 smarty 的 PHP 循环

php - 在 Magento 中使用缩略图切换基本图像

php - 使用常规登录密码保护 Magento Storeview

php - php中通过NAT识别计算机

php - 连接字符串或使用多个回显参数 : which is faster?