Search code examples
classmagentomoduleblockfatal-error

Magento: error for custom module (Class not found in Layout.php)


I tried to create a new custom module (block) in Magento which will show other products from manufacturer on product detail page. When I load product detail page I get:

Fatal error: Class 'AimIT_ManufacturerBlock_Block_Manufacturerblock' not found in ..\app\code\core\Mage\Core\Model\Layout.php on line 491

I have created:

1)\app\etc\modules\AimIT_ManufacturerBlock.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
  <modules>
    <AimIT_ManufacturerBlock>
      <!-- Whether our module is active: true or false -->
        <active>true</active>
        <!-- Which code pool to use: core, community or local -->
          <codePool>local</codePool>
        </AimIT_ManufacturerBlock>
      </modules>
    </config>

2) \app\code\local\AimIT\ManufacturerBlock\etc\config.xml

<?xml version="1.0"?>
<config>
  <global>
    <blocks>
      <aimitmanufacturerblock>
        <class>AimIT_ManufacturerBlock_Block</class>
      </aimitmanufacturerblock>
    </blocks>
  </global>
</config>

3) \app\code\local\AimIT\ManufacturerBlock\Block\Manufacturerblock.php

<?php
class AimIT_ManufacturerBlock_Block_Manufacturerblock extends Mage_Core_Block_Template 
{    
    public function getManufacturerProducts($manufacturer)
    {
        $collection = Mage::getModel('catalog/product')->getCollection();
        $collection->addAttributeToFilter('manufacturer',$manufacturer);
        $collection->addAttributeToSelect('manufacturer');

        return $collection;
    }
}
?>

4)\app\design\frontend\default\respond\template\aimit\manufacturerblock\manufacturerblock.phtml

<?php $_products = $this->getManufacturerProducts('cukrarna-u-vanku') ?>
<?php print_r($_products); ?>

5) in catalog\product\view.phtml I have placed this code:

<?php echo $this->getLayout()->createBlock('aimitmanufacturerblock/manufacturerblock')->setTemplate('aimitmanufacturerblock/manufacturerblock.phtml')->toHtml(); ?>

What did I omit while creating the module?


Solution

  • When translating 'aimitmanufacturerblock/manufacturerblock' into a class name Magento generates AimIT_ManufacturerBlock_Block_Manufacturerblock and can't find a class under such name because your block's class name is actually 'AimIT_ManufacturerBlock_Block_ManufacturerBlock' - which is wrongly cased.

    Rename your class into

    class AimIT_ManufacturerBlock_Block_Manufacturerblock extends Mage_Core_Block_Template 
    {
    

    Rename your class file ManufacturerBlock.php into Manufacturerblock.php