Magento自定义模块,致命错误:调用成员函数setFormAction()

时间:2009-07-08 14:39:38

标签: php xml layout magento controller

当我在此网址http://localhost/xxx/index.php/TradeEnquiry上点击我的模块时出现此错误

  

致命错误:调用成员函数   setFormAction()在非对象中   C:\瓦帕\ WWW \ stockdisplays \应用\代码\本地\库存\ Tradeenquiry \控制器\ IndexController.php   在第55行

第55行是这样的:

 $this->getLayout()->getBlock('tradeenquiryView')
            ->setFormAction( Mage::getUrl('*/*/post') );

这是我的布局xml中的一个片段:

<default>
    <reference name="footer_links">
        <action method="addLink" translate="label title" module="tradeenquiry">
                <label>Trade Enquiry</label>
                <url>tradeenquiry</url>
                <title>Trade Enquiry</title>
                <prepare>true</prepare>
            </action>
    </reference>
</default>

<tradeenquiry_index_index>
    <reference name="root">
        <action method="setTemplate"><template>page/2columns-right.phtml</template></action>
        <action method="setHeaderTitle" translate="title" module="tradeenquiry"><title>Trade Enquiry</title></action>
    </reference>
    <reference name="content">
        <block type="core/template" name="tradeenquiryView" template="tradeenquiry/view.phtml"/>
    </reference>
</tradeenquiry_index_index>

我不知道问题是什么?该块正确命名为“tradeenquiryView”。我唯一能想到的是布局xml以某种方式缓存?因为我必须点击/ TradeEnquiry上的模块,而不是像我在布局xml中所说的那样/ tradeenquiry,所以它几乎就像它使用旧版本一样?

1 个答案:

答案 0 :(得分:8)

像Magento一样,有很多原因可能会发生。

首先,在命名块时我会避免使用大写字母“V”。虽然我不认为这会导致问题(因为名称是对URI(核心/文本列表等)和模板文件路径(/path/to/template.phtml)的引用),所以有一个在小写/下划线名称的布局系统中事实上的命名约定,某人(即Varien)可能决定的是强制约定。

其次,你打电话了吗

$this->loadLayout();

在您尝试设置表单操作的行之前的控制器操作中?在您执行此操作之前,您的Layout对象将不会实例化和/或具有对块对象的引用,这意味着

$this->getLayout()->getBlock('...')

将始终返回false。

其他调试提示。请尝试以下操作,以确保您获得了您认为应该的课程。

die(get_class($this->getLayout()->getBlock('root')));                                   
die(get_class($this->getLayout()));         

最后,如果全部失败,请转到布局对象的来源

app/code/core/Mage/Core/Model/Layout.php

看看getBlock方法

public function getBlock($name)
{
    if (isset($this->_blocks[$name])) {
        return $this->_blocks[$name];
    } else {
        return false;
    }
}

并开始抛出调试语句,看看是否可以找出系统没有返回对块的引用的原因。不要忘记删除/不签入调试语句,因为这是核心系统代码。