【问题标题】:'Mage_Core_Exception' with message 'Invalid block type: in /var/www/magento1/app/Mage.php:595'Mage_Core_Exception' 带有消息'无效块类型:在 /var/www/magento1/app/Mage.php:595
【发布时间】:2014-07-25 09:28:44
【问题描述】:

我在创建一个简单的 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>

请告诉我我错过了其中的内容。提前谢谢..

【问题讨论】:

    标签: magento


    【解决方案1】:

    阿米特·贝拉(Amit Bera)走在正确的轨道上。然而,他的回答并不完全正确。类和文件名可以包含大写字母,但是这个字母在布局中也应该是大写的。类名的第一个字母是一个例外——它在类名中总是大写,在布局中总是小写。

    如果类名是 HelloWorld,那么布局中的块类型应该是 helloworld/helloWorld,如下所示:

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

    【讨论】:

    • 我最近遇到了同样的问题,我的开发环境由于某种原因不关心这个案例,当我将它放入我的测试服务器时,这个问题悄悄出现了。现在只要我知道为什么我的虚拟机不关心这个案子就好了。
    【解决方案2】:

    车坛,你的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 
    

    【讨论】:

    • 谢谢,只是我想再问一件小事,我的块类名称“MyCompanyName_HelloWorld_Block_HelloWorld”是否正确,或者应该是“MyCompanyName_HelloWorld_Block_Helloworld”?我只是将我的 php 文件名 HelloWorld 更改为 Helloworld
    • MyCompanyName_HelloWorld_Block_Helloworld 是对的。Block 类名取决于文件名
    猜你喜欢
    • 2017-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-01
    • 2016-08-21
    相关资源
    最近更新 更多