我创建了一个新模块,我在IndexController.php
app/code/local/Ashfame/Helloblock/controllers/IndexController.php
中通过以下方法创建了一个块。{/ 1}}
class Ashfame_Helloblock_IndexController extends Mage_Core_Controller_Front_Action {
public function indexAction() {
// this works fine
$block = new Ashfame_Helloblock_Block_Helloblock();
echo $block->toHtml();
}
public function layoutAction() {
// this doesn't work
$layout = Mage::getSingleton('core/layout');
$block = $layout->createBlock('ashfame_helloblock/helloblock','root');
var_dump( $block ); // this returns false
echo $block->toHtml();
}
}
我的Block类为Ashfame_Helloblock_Block_Helloblock
,位于app/code/local/Ashfame/Helloblock/Block/Helloblock.php
由于$block
中的layoutAction()
为false,因此PHP会引发错误Call to a member function toHtml() on a non-object
在调试时我发现magento正在弄清楚类名错误。它在mage_Ashfame_helloblock_block
内找出了getGroupedClassName()
的班级名称。
这里有什么问题?
答案 0 :(得分:2)
我认为问题出在您的配置文件中。在app/code/local/Ashfame/Helloblock/etc/config.xml
,您应该:
<config>
...
<global>
...
<blocks>
<ashfame_helloblock>
<class>Ashfame_Helloblock_Block</class>
</ashfame_helloblock>
</blocks>
</global>
</config>