我正在运行 Magento 1.7.0.2(在 Ubuntu Linux 12.04 LTS 上)。
我正在尝试获取受 Alan Storm 的 No Frills Magento Layout book 1.8 节启发的 Magento 代码部分。首先,我认为该部分的 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 类型的对象,这就是我所追求的。以下是我的自定义文件的内容和触发 URL:
触发错误的 URL:
http:// localhost/magpractice/helloworld/index/layout/
/var/www/magpractice/app/code/local/Alanstormdotcom/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();
}
}
?>
/var/www/magpractice/app/code/local/Alanstormdotcom/Helloworld/Block/Hello.php
<?php
class Alanstormdotcom_Helloworld_Block_Hello extends Mage_Core_Block_Template {
public function _construct() {
$this->setTemplate('helloworld.phtml');
return parent::_construct();
}
public function _beforeToHtml() {
$block_1 = new Mage_Core_Block_Text();
$block_1->setText('The first sentence.');
$this->setChild('the_first', $block_1);
}
public function fetchTitle() {
return 'Hello Fancy World!';
}
}
?>
/var/www/magpractice/app/code/local/Alanstormdotcom/Helloworld/etc/config.xml
<config>
<modules>
<Alanstormdotcom_Helloworld>
<version>0.1.0</version>
</Alanstormdotcom_Helloworld>
</modules>
<frontend>
<routers>
<helloworld>
<use>standard</use>
<args>
<module>Alanstormdotcom_Helloworld</module>
<frontName>helloworld</frontName>
</args>
</helloworld>
</routers>
</frontend>
</config>
以下是 /var/www/magpractice/var/log 的内容:
2013-06-11T12:42:56+00:00 ERR (3):
exception 'Mage_Core_Exception' with message 'Invalid block type: Mage_Alanstormdotcom_Helloworld_Block_Hello' in /var/www/magpractice/app/Mage.php:594
Stack trace:
#0 /var/www/magpractice/app/code/core/Mage/Core/Model/Layout.php(495): Mage::throwException('Invalid block t...')
#1 /var/www/magpractice/app/code/core/Mage/Core/Model/Layout.php(437): Mage_Core_Model_Layout->_getBlockInstance('alanstormdotcom...', Array)
#2 /var/www/magpractice/app/code/local/Alanstormdotcom/Helloworld/controllers/IndexController.php(38): Mage_Core_Model_Layout->createBlock('alanstormdotcom...', 'root')
#3 /var/www/magpractice/app/code/core/Mage/Core/Controller/Varien/Action.php(419): Alanstormdotcom_Helloworld_IndexController->layoutAction()
#4 /var/www/magpractice/app/code/core/Mage/Core/Controller/Varien/Router/Standard.php(250): Mage_Core_Controller_Varien_Action->dispatch('layout')
#5 /var/www/magpractice/app/code/core/Mage/Core/Controller/Varien/Front.php(176): Mage_Core_Controller_Varien_Router_Standard->match(Object(Mage_Core_Controller_Request_Http))
#6 /var/www/magpractice/app/code/core/Mage/Core/Model/App.php(354): Mage_Core_Controller_Varien_Front->dispatch()
#7 /var/www/magpractice/app/Mage.php(683): Mage_Core_Model_App->run(Array)
#8 /var/www/magpractice/index.php(88): Mage::run('', 'store')
#9 {main}
有人可以指出我的代码哪里出了问题,或者说明 magento 1.7.0.2 在 alan Storm nofrills magento 布局文档中描述的内容(除此之外一直是一本优秀的书)中所描述的内容有何偏差?
谢谢。