【问题标题】:Magento rewrite Core/Block/Template.php (Magento CE 1.8.1.0)Magento 重写 Core/Block/Template.php (Magento CE 1.8.1.0)
【发布时间】:2014-05-09 18:16:34
【问题描述】:

为了重写getTemplateFile()函数,我需要重写一个完整路径为app/code/core/Mage/Core/Block/Template.php的Magento核心块文件。 我重写它们没有问题,但结果很奇怪。在getTemplateFile() 函数中,我有$this->getTemplate(),它返回一个模板。当我在核心块函数中print_r($this->getTemplate()) 时,它给了我所有正在加载的 PHTML,而在重写的块中我只得到了其中的一些。你有什么想法为什么会这样?我正在使用 Magento CE 1.8.1.0。

【问题讨论】:

    标签: php magento rewrite magento-1.8


    【解决方案1】:

    您不能在 Magento 中重写块/模型/助手的父类,您只能重写特定实例。

    Magento 的类重写是这样工作的。每当核心 Magento 代码(和编码良好的模块)实例化一个对象时,它们都会使用工厂方法来实现。

    $layout->createBlock('core/template');
    

    core/template 字符串是类别名。 Magento 使用它来查找要使用的实际类名。默认情况下,这是Mage_Core_Block_Template。当你创建一个重写时,你是在告诉 Magento

    嘿,每当您创建 core/template 块对象时?改用我的课。

    问题? Magento 中有许多块从core/template继承。当 Magento 实例化其中之一时

    $layout->createBlock('page/html_head')
    

    解析为类Mage_Page_Block_Html_Head,继承自核心模板类

    #File: app/code/core/Mage/Page/Block/Html/Head.php
    class Mage_Page_Block_Html_Head extends Mage_Core_Block_Template
    {
    }
    

    换句话说,重写系统无法知道您在core/template 类中的交换。

    所以当你说

    在核心块函数中,它为我提供了所有正在加载的 PHTML,而在重写的块中,我只得到其中一些

    听起来您正在查看 core/template 块的 PHTML,但任何从 Mage_Core_Block_Template 继承的块都将被跳过。

    【讨论】:

      猜你喜欢
      • 2014-04-16
      • 2014-06-25
      • 1970-01-01
      • 2013-05-25
      • 1970-01-01
      • 2012-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多