1

I created a new module, in which I am creating a block by the following method in IndexController.php which is at app/code/local/Ashfame/Helloblock/controllers/IndexController.php

class Ashfame_Helloblock_IndexController extends Mage_Core_Controller_Front_Action {

    public function indexAction() {
        // this works fine
        $block = new Ashfame_Helloblock_Block_Helloblock();
        echo $block->toHtml();
    }

    public function layoutAction() {
        // this doesn't work
        $layout = Mage::getSingleton('core/layout');
        $block = $layout->createBlock('ashfame_helloblock/helloblock','root');
        var_dump( $block ); // this returns false
        echo $block->toHtml();
    }
}

My Block class is Ashfame_Helloblock_Block_Helloblock and is at app/code/local/Ashfame/Helloblock/Block/Helloblock.php

Since the $block in layoutAction() is false, PHP throws an error Call to a member function toHtml() on a non-object

On debugging I found that the magento is figuring out the class name wrong. It figure out the class name as mage_Ashfame_helloblock_block inside getGroupedClassName().

What's wrong here?

3
  • What does your app/code/local/Ashfame/Helloblock/etc/config.xml look like? Commented Apr 1, 2012 at 18:54
  • 1
    Good job tracing through the classname mapping. An explorer's heart is so important in Magento development. Commented Apr 1, 2012 at 20:49
  • @benmarks heh thanks! I second that :) Commented Apr 2, 2012 at 4:10

1 Answer 1

2

I think the problem is in your configuration file. In app/code/local/Ashfame/Helloblock/etc/config.xml you should have:

<config>
  ...
  <global>
    ...
    <blocks>
        <ashfame_helloblock>
             <class>Ashfame_Helloblock_Block</class>
        </ashfame_helloblock>
    </blocks>
  </global>
</config>
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! That did it, just that <ashfame_helloblock> needs to be <Ashfame_Helloblock> for it to work :)
I take that comment back. Must have been something else at that time. This needs to be in lowercase as shown in the sample code block.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.