无虚饰Magento布局:createBlock抛出异常;无效的块类型";
我正在运行Magento 1.7.0.2(在Ubuntu Linux 12.04 LTS上) 我正试图获得一段Magento代码,灵感来自Alan Storm的无装饰Magento布局书第1.8节。首先,我认为该部分的Mage_Core_Template_Block应该是Mage_Core_Block_Template 但更重要的是,我对$block=$layout->createBlock('foo_-bar/hello')的调用返回false,并在exception.log中引发异常,因为它在foo前面加了Mage,因此引用了一个名为Mage_-foo_-bar_-block_-hello的对象,该对象不存在,而不是我要找的foo_-bar_-block_-hello类型的对象中的return。以下是我的自定义文件的内容和触发URL:无虚饰Magento布局:createBlock抛出异常;无效的块类型";,magento,magento-1.7,Magento,Magento 1.7,我正在运行Magento 1.7.0.2(在Ubuntu Linux 12.04 LTS上) 我正试图获得一段Magento代码,灵感来自Alan Storm的无装饰Magento布局书第1.8节。首先,我认为该部分的Mage_Core_Template_Block应该是Mage_Core_Block_Template 但更重要的是,我对$block=$layout->createBlock('foo_-bar/hello')的调用返回false,并在exception.log中引发异常,因为它
触发错误的URL:
http:// localhost/magpractice/helloworld/index/layout/
/var/www/magpracture/app/code/local/alanstromdotcom/Helloworld/controllers/IndexController.php
<?php
class Alanstormdotcom_Helloworld_IndexController extends Mage_Core_Controller_Front_Action {
function layoutAction() {
$layout = Mage::getSingleton('core/layout');
$block = $layout->createBlock('alanstormdotcom_helloworld/hello', 'root');
var_dump($block);
echo $block->toHtml();
}
}
?>
有人能指出我的代码哪里出了问题,或者说magento 1.7.0.2在alan storm nofrills magento布局文档(除此之外,这是一本很好的书)中描述的内容中可能会出现一些偏差吗
谢谢。您的
config.xml
文件不完整。应该是这样的:
<config>
<modules>
<Alanstormdotcom_Helloworld>
<version>0.1.0</version>
</Alanstormdotcom_Helloworld>
</modules>
<!-- Missing blocks and helpers definition here: -->
<global>
<blocks>
<helloworld>
<class>Alanstormdotcom_Helloworld_Block</class>
</helloworld>
</blocks>
<helpers>
<helloworld>
<class>Alanstormdotcom_Helloworld_Block</class>
</helloworld>
</helpers>
</global>
<frontend>
<routers>
<helloworld>
<use>standard</use>
<args>
<module>Alanstormdotcom_Helloworld</module>
<frontName>helloworld</frontName>
</args>
</helloworld>
</routers>
</frontend>
</config>
0.1.0
Alanstormdotcom_Helloworld_街区
Alanstormdotcom_Helloworld_街区
标准
Alanstormdotcom_Helloworld
地狱世界
您的config.xml
文件不完整。应该是这样的:
<config>
<modules>
<Alanstormdotcom_Helloworld>
<version>0.1.0</version>
</Alanstormdotcom_Helloworld>
</modules>
<!-- Missing blocks and helpers definition here: -->
<global>
<blocks>
<helloworld>
<class>Alanstormdotcom_Helloworld_Block</class>
</helloworld>
</blocks>
<helpers>
<helloworld>
<class>Alanstormdotcom_Helloworld_Block</class>
</helloworld>
</helpers>
</global>
<frontend>
<routers>
<helloworld>
<use>standard</use>
<args>
<module>Alanstormdotcom_Helloworld</module>
<frontName>helloworld</frontName>
</args>
</helloworld>
</routers>
</frontend>
</config>
0.1.0
Alanstormdotcom_Helloworld_街区
Alanstormdotcom_Helloworld_街区
标准
Alanstormdotcom_Helloworld
地狱世界
1.7.0.2中没有影响代码的差异-您的config.xml
缺少其
部分
您正在实例化一个名为alanstormdotcom\u helloworld/hello
的块。这是一个组名alanstormdotcom\u helloworld
,一个类名hello
但是,您的模块没有块
配置部分。没有本节,Magento不知道alanstormdotcom\u helloworld
组中的块使用什么PHP类名。因为它不知道,所以它假设你的区块是法师核心的一部分,并对一个名字进行猜测(Mage\u alanstromdotcom\u Helloworld\u block\u Hello
)
查看工作模块如何配置其
部分。这会让你找到正确的方向
如果不检查我的工作,您的配置应该如下所示
<config>
<!-- ... -->
<global>
<!-- ... -->
<blocks>
<alanstormdotcom_helloworld>
<class>Alanstormdotcom_Helloworld_Block</class>
</alanstormdotcom_helloworld>
</blocks>
</global>
</config>
Alanstormdotcom_Helloworld_街区
如果仍有问题,请尝试将代码追溯到Magento从类别名创建类名的位置(
alanstormdotcom\u helloworld/hello
)。除了解决yoru问题,这对于新的Magento程序员来说总是一个很好的练习 1.7.0.2中没有影响代码的差异-您的config.xml
缺少
部分
您正在实例化一个名为alanstormdotcom\u helloworld/hello
的块。这是一个组名alanstormdotcom\u helloworld
,一个类名hello
但是,您的模块没有块
配置部分。没有本节,Magento不知道alanstormdotcom\u helloworld
组中的块使用什么PHP类名。因为它不知道,所以它假设你的区块是法师核心的一部分,并对一个名字进行猜测(Mage\u alanstromdotcom\u Helloworld\u block\u Hello
)
查看工作模块如何配置其
部分。这会让你找到正确的方向
如果不检查我的工作,您的配置应该如下所示
<config>
<!-- ... -->
<global>
<!-- ... -->
<blocks>
<alanstormdotcom_helloworld>
<class>Alanstormdotcom_Helloworld_Block</class>
</alanstormdotcom_helloworld>
</blocks>
</global>
</config>
Alanstormdotcom_Helloworld_街区
如果仍有问题,请尝试将代码追溯到Magento从类别名创建类名的位置(
alanstormdotcom\u helloworld/hello
)。除了解决yoru问题,这对于新的Magento程序员来说总是一个很好的练习 您好,我通过更改config.xml尝试了您的建议,但仍然得到bool(false)致命错误:在第40行的/var/www/magprace/app/code/local/alansturdotcom/Helloworld/controllers/IndexController.php中对非对象调用成员函数toHtml()——第40行是echo$block->toHtml();$block设置为false后。如果将控制器中的第40行替换为:$block=$layout->createBlock('helloworld/hello','root')代码>?如果我用helloworld替换alanstormdotcom_helloworld,正如您在这里所描述的,那么它可以工作,但我需要两个块和内部的helpers标记。。。标签,否则我会出错。你能描述一下它们的作用和需要它们的原因吗?Alan刚刚回答了自己的问题,你会在那里找到正确的解释和实际的解决方案,使一切都能工作,而不必改变示例控制器。我没有这本书,所以我无法回答你,但是在这里的回答中,他还指出了应用于config.xml
文件的更改,以及为什么它不能以其他方式工作您好,我尝试了您的建议,更改了config.xml,但仍然得到bool(false)致命错误:调用成员函数toHtml()在/var/www/magpracture/app/code/local/alanstromdotcom/Helloworld/controllers/IndexCo中的非对象上
<config>
<!-- ... -->
<global>
<!-- ... -->
<blocks>
<alanstormdotcom_helloworld>
<class>Alanstormdotcom_Helloworld_Block</class>
</alanstormdotcom_helloworld>
</blocks>
</global>
</config>