在Magento中实例化(而不是对象)后导致块为false的原因


What causes a block to be false after instantiating (and not an object) in Magento?

我一直在为我的Magento模块在adminhtml中获取一个新表单。然而,我在实际加载块时遇到了一个严重的问题。我可以使用我在另一个堆栈溢出问题中发现的代码加载测试块(使用phtml布局)(我在adminhtml控制器中实现了这一点):

    $block = $this->getLayout()->createBlock('coupsmart_coupon/adminhtml_forms');
    error_log('The block:' . var_export($block, true));
    if($block)
    {
        $block->setTemplate('test/test.phtml');
        error_log(var_export($block->getTemplate(), true));
        error_log('The HTML:');
        error_log(var_export($block->toHtml(),true));   
    }

使用一个测试块,我得到了正确的html(在我的adminhtml/default/default/template文件夹中找到)。

然而,当我实例化一个grid_container块时,它不会运行if($block){}部分,因为该块是false。但在我的grid_container的块类中,我有一个__constructor()方法,我在其中记录输出,所以它正在运行构造函数,这意味着我的类实例化(和命名)是正确的。

对于Mage_Adminhtml_Block_Widget_Grid_Container类,是什么导致构造函数在块上运行,但仍然返回false?

如果需要更多的代码(控制器、网格容器块、网格块、配置等),请告诉我,我会发布它。我只是不想被代码溢出淹没,这可能会淡化这个问题。

编辑:网格容器

class Coupsmart_Coupon_Block_Adminhtml_Forms extends
Mage_Adminhtml_Block_Widget_Grid_Container
{       
    public function __construct()
    {
        error_log('adminhtml forms (parent) construct');
        $this->_controller = 'adminhtml_forms';
        $this->_blockGroup = 'coupsmart_coupon';
        $this->_headerText = Mage::helper('forms')->__('Coupon Manager');
        $this->_addButtonLabel = Mage::helper('forms')->__('Edit Coupon');
        parent::__construct();
    }
}

当我实例化块时,上面容器中的错误日志就会显示出来。

如果在实例化时抛出异常,则返回的块可能为false。在您的代码中,这肯定来自这一部分:Mage::helper('forms')
在模块的config.xml文件中,您是否定义了这样的助手?:

<global>
    <helpers>
        <forms><class>Coupsmart_Coupon_Helper</class></forms>
    </helpers>
</global>

否则,将forms替换为调用Mage::helper('forms')时使用的其他代码(从外观上看,这可能看起来像这样:Mage::helper('coupsmart_coupon')