当我创建一个简单的hello world模块时,我在日志文件中遇到了这个异常。
exception 'Mage_Core_Exception' with message 'Invalid block type: MyCompanyName_HelloWorld_Block_Helloworld' in /var/www/magento1/app/Mage.php:595
Stack trace:
#0 /var/www/magento1/app/code/core/Mage/Core/Model/Layout.php(495): Mage::throwException('Invalid block t...')
#1 /var/www/magento1/app/code/core/Mage/Core/Model/Layout.php(437): Mage_Core_Model_Layout->_getBlockInstance('helloworld/hell...', Array)
#2 /var/www/magento1/app/code/core/Mage/Core/Model/Layout.php(472): Mage_Core_Model_Layout->createBlock('helloworld/hell...', 'helloworld.hell...')
#3 /var/www/magento1/app/code/core/Mage/Core/Model/Layout.php(239): Mage_Core_Model_Layout->addBlock('helloworld/hell...', 'helloworld.hell...')
#4 /var/www/magento1/app/code/core/Mage/Core/Model/Layout.php(205): Mage_Core_Model_Layout->_generateBlock(Object(Mage_Core_Model_Layout_Element), Object(Mage_Core_Model_Layout_Element))
#5 /var/www/magento1/app/code/core/Mage/Core/Model/Layout.php(210): Mage_Core_Model_Layout->generateBlocks(Object(Mage_Core_Model_Layout_Element))
#6 /var/www/magento1/app/code/core/Mage/Core/Controller/Varien/Action.php(344): Mage_Core_Model_Layout->generateBlocks()
#7 /var/www/magento1/app/code/core/Mage/Core/Controller/Varien/Action.php(269): Mage_Core_Controller_Varien_Action->generateLayoutBlocks()
#8 /var/www/magento1/app/code/local/MyCompanyName/HelloWorld/controllers/IndexController.php(5): Mage_Core_Controller_Varien_Action->loadLayout()
#9 /var/www/magento1/app/code/core/Mage/Core/Controller/Varien/Action.php(418): MyCompanyName_HelloWorld_IndexController->indexAction()
#10 /var/www/magento1/app/code/core/Mage/Core/Controller/Varien/Router/Standard.php(250): Mage_Core_Controller_Varien_Action->dispatch('index')
#11 /var/www/magento1/app/code/core/Mage/Core/Controller/Varien/Front.php(172): Mage_Core_Controller_Varien_Router_Standard->match(Object(Mage_Core_Controller_Request_Http))
#12 /var/www/magento1/app/code/core/Mage/Core/Model/App.php(354): Mage_Core_Controller_Varien_Front->dispatch()
#13 /var/www/magento1/app/Mage.php(684): Mage_Core_Model_App->run(Array)
#14 /var/www/magento1/index.php(87): Mage::run('', 'store')
#15 {main}
配置文件etc / config.xml
<?xml version="1.0"?>
<config>
<modules>
<MyCompanyName_HelloWorld>
<version>0.0.1</version>
</MyCompanyName_HelloWorld>
</modules>
<global>
<blocks>
<helloworld>
<class>MyCompanyName_HelloWorld_Block</class>
</helloworld>
</blocks>
</global>
<frontend>
<routers>
<helloworld>
<use>standard</use>
<args>
<module>MyCompanyName_HelloWorld</module>
<frontName>helloworld</frontName>
</args>
</helloworld>
</routers>
<layout>
<updates>
<helloworld>
<file>helloworld.xml</file>
</helloworld>
</updates>
</layout>
</frontend>
</config>
布局文件helloworld.xml
<?xml version="1.0"?>
<layout version="0.1.0">
<helloworld_index_index>
<reference name="root">
<action method="setTemplate"><template>page/1column.phtml</template></action>
</reference>
<reference name="content">
<!--<block type="core/template" name="helloworld" template="helloworld/helloworld.phtml"/> -->
<block type="helloworld/helloworld" name="helloworld.helloworld" template="helloworld/helloworld.phtml"/>
</reference>
</helloworld_index_index>
</layout>
阻止类文件HelloWorld.php
class MyCompanyName_HelloWorld_Block_HelloWorld extends Mage_Core_Block_Template {
}
controlers / indexController.php
class MyCompanyName_HelloWorld_IndexController extends Mage_Core_Controller_Front_Action {
public function indexAction() {
//echo "We're echoing just to show that this is what's called, normally you'd have some kind of redirect going on here";
$this->loadLayout();
$this->renderLayout();
}
}
app / etc / module MyCompanyName_HelloWorld.xml
<?xml version="1.0"?>
<config>
<modules>
<MyCompanyName_HelloWorld>
<active>true</active>
<codePool>local</codePool>
</MyCompanyName_HelloWorld>
</modules>
</config>
请告诉我我错过了哪里的东西。提前谢谢..
答案 0 :(得分:3)
Amit Bera走在正确的轨道上。但他的回答并不完全正确。类和文件名可以包含大写字母,但是这个字母在布局中也应该是大写。类名的第一个字母是一个例外 - 它在类名中始终为大写,在布局中始终为小写。
在你的情况下,如果类名是HelloWorld,那么布局中的块类型应该是helloworld / helloWorld ,如下所示:
<block type="helloworld/helloWorld" name="helloworld.helloworld" template="helloworld/helloworld.phtml"/>
答案 1 :(得分:1)
Chetan,你的block class name is wrong
:
它应该是Helloworld.php instead of HelloWorld.php.
W should be small letter
根据magento,如果任何类文件在文件名中有两个大写字母,则应该显示为一个目录和一个文件。
示例
In MyCompanyName_HelloWorld_Block_HelloWorld the class name after
MyCompanyName_HelloWorld_Block is Hellword,Magenti split class
into Hello is dir Word the class file