0

当我尝试在控制器中加载我的块时出现此错误

致命错误:在...中的非对象上调用成员函数 toHtml() 在线...

这是我的 config.xml

<blocks>
  <auction>
    <class>Custom_Auction_Block</class>
  </auction>
</blocks>

我在控制器中使用以下代码调用我的块:

$this->loadLayout();
$autoupdate= $this->getLayout()->getBlock('auction.auto.update')->toHtml();
Mage::register('referrer_url', $this->_getRefererUrl());
$response['autoupdate'] = $autoupdate;

我在拍卖/块/产品/autoupdate.php 中创建块

class Custom_Auction_Block_Product_Autoupdate extends Mage_Core_Block_Template{   

}

这是我的布局

<block type="auction/product_view" name="auction_view" template="auction/catalog/product/view.phtml">
        <block type="auction/product_autoupdate" name="auction.auto.update" as="auction_autoupdate" template="auction/catalog/product/autoupdate.phtml"/>
  </block>

我也尝试在我的块中添加“受保护的函数_toHtml()”。但它不起作用。

任何人都可以帮助我吗?仅供参考,我提前使用 magento 1.6.2.0 Thx :)

4

1 回答 1

2

您的来电

$this->getLayout()->getBlock('auction.auto.update')

没有返回块对象。那是因为 Magento 没有为您运行代码的特定页面运行布局 XML 更新,或者因为它无法实例化具有类 alias 的块auction/product_autoupdate

我有根据的猜测是后者,您的代码和配置示例太不精确,无法确定原因。尝试运行以下代码

$b = $this->getLayout()->createBlock('auction/product_autoupdate');
var_dump($b);

如果var_dump没有转储对象,则表示您的配置不正确。开始跟踪createBlock方法中的代码,直到它引用配置以确定块的 PHP 类。这应该告诉您配置有什么问题。祝你好运。

于 2013-05-06T06:13:28.650 回答