2014-07-25 25 views
0

當我剛創建一個簡單的Hello World模塊時,我在日誌文件中得到了這個異常。'Mage_Core_Exception'帶有消息'Invalid block type:in /var/www/magento1/app/Mage.php:595

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} 

配置文件等等/ 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> 

Block類文件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(); 
    } 
} 

應用程序的/ etc /模塊MyCompanyName_HelloWorld.xml

<?xml version="1.0"?> 
<config> 
    <modules> 
     <MyCompanyName_HelloWorld> 
      <active>true</active> 
      <codePool>local</codePool> 
     </MyCompanyName_HelloWorld> 
    </modules> 
</config> 

請告訴我在哪裏,我在它錯過了的東西。在此先感謝..

回答

2

阿米特貝拉是在正確的軌道。但是他的回答並不完全正確。類和文件名可以包含大寫字母,但這個字母在佈局中也應該是大寫。類名的第一個字母是一個例外 - 在類名中總是大寫,在佈局中總是小寫。

在如果類名是HelloWorld的,所以在佈局塊型應該的HelloWorld/的helloWorld你這樣的情況:

<block type="helloworld/helloWorld" name="helloworld.helloworld" template="helloworld/helloworld.phtml"/> 
+0

更多專家答覆.....謝謝alexei –

+0

我最近有同樣的問題,我的開發環境不關心這種情況出於某種原因,當我把它放到我的測試服務器上時,這個問題悄然升起。現在,如果我只知道我的虛擬機爲什麼不關心這個情況。 –

0

扯談,你block class name is wrong

應該是Helloworld.php instead of HelloWorld.php.

W should be small letter 

據Magento的,如果任何類文件中有兩個上字母的文件名是否應該explit到一個目錄和一個文件。

例如

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 
+0

感謝阿米特 只是我想再問一個很小的事情,我的塊級名稱'MyCompanyName_HelloWorld_Block_HelloWorld'是否正確或者它應該是「MyCompanyName_HelloWorld_Block_Helloworld」? 我只是改變我的php文件名HelloWorld Helloworld –

+0

MyCompanyName_HelloWorld_Block_Helloworld是正確的。塊類名是取決於文件名 –