Magento – fatal error : call to a member function toHtml() on boolean

ajaxfatal errormagento-1.9

fatal error : call to a member function toHtml() on boolean in
D:\Projects\Php\Dev6\app\code\community\Foo\Bar\controllers\Adminhtml\BazController.php
on line 140

Here is my code :

<?php
class Foo_Bar_Adminhtml_BazController extends Mage_Adminhtml_Controller_Action
{
    public function indexAction()
    {  
    // Let's call our initAction method which will set some basic params for each action
    $this->_initAction()
        ->renderLayout();
}  

public function newAction()
{  
    // We just forward the new action to a blank edit form
    $this->_forward('edit');
}  

public function editAction()
{  
    $this->_initAction();

    // Get id if available
    $id  = $this->getRequest()->getParam('id');
    $model = Mage::getModel('foo_bar/baz');

    if ($id) {
        // Load record
        $model->load($id);

        // Check if record is loaded
        if (!$model->getId()) {
            Mage::getSingleton('adminhtml/session')->addError($this->__('This baz no longer exists.'));
            $this->_redirect('*/*/');

            return;
        }  
    }  

    $this->_title($model->getId() ? $model->getName() : $this->__('New Employee'));

    $data = Mage::getSingleton('adminhtml/session')->getBazData(true);
    if (!empty($data)) {

        /*if (is_array($data['expertise'])) {
            $data['expertise'] = implode(',', $data['expertise']);
        }*/

        $model->setData($data);
    }  

    Mage::register('foo_bar', $model);

    $this->_initAction()
        ->_addBreadcrumb($id ? $this->__('Edit Employee') : $this->__('New Employee'), $id ? $this->__('Edit Employee') : $this->__('New Employee'))
        ->_addContent($this->getLayout()->createBlock('foo_bar/adminhtml_baz_edit')->setData('action', $this->getUrl('*/*/save')))
        ->renderLayout();
}

public function saveAction()
{
    //var_dump($postData);
    if ($postData = $this->getRequest()->getPost()) {
        $model = Mage::getSingleton('foo_bar/baz');
        $model->setData($postData);

        try {
            $model->save();

            Mage::getSingleton('adminhtml/session')->addSuccess($this->__('The baz has been saved.'));
            $this->_redirect('*/*/');

            return;
        }  
        catch (Mage_Core_Exception $e) {
            Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
        }
        catch (Exception $e) {
            Mage::getSingleton('adminhtml/session')->addError($this->__('An error occurred while saving this baz.'));
        }

        Mage::getSingleton('adminhtml/session')->setBazData($postData);
        $this->_redirectReferer();
    }
}

public function messageAction()
{
    $data = Mage::getModel('foo_bar/baz')->load($this->getRequest()->getParam('id'));
    echo $data->getContent();
}

/**
 * Initialize action
 *
 * Here, we set the breadcrumbs and the active menu
 *
 * @return Mage_Adminhtml_Controller_Action
 */
protected function _initAction()
{
    $this->loadLayout()
        // Make the active menu match the menu config nodes (without 'children' inbetween)
        ->_setActiveMenu('system/foo_bar_baz')
        ->_title($this->__('System'))->_title($this->__('Employee'))
        ->_addBreadcrumb($this->__('System'), $this->__('System'))
        ->_addBreadcrumb($this->__('Employee'), $this->__('Employee'));

    return $this;
}

 public function massDeleteAction()
        {
        $taxIds = $this->getRequest()->getParam('tax_id');      // $this->getMassactionBlock()->setFormFieldName('tax_id'); from Mage_Adminhtml_Block_Tax_Rate_Grid
        if(!is_array($taxIds)) {
        Mage::getSingleton('adminhtml/session')->addError(Mage::helper('tax')->__('Please select tax(es).'));
        } else {
        try {
        $rateModel = Mage::getModel('tax/calculation_rate');
        foreach ($taxIds as $taxId) {
        $rateModel->load($taxId)->delete();
        }
        Mage::getSingleton('adminhtml/session')->addSuccess(
        Mage::helper('tax')->__(
        'Total of %d record(s) were deleted.', count($taxIds)
        )
        );
        } catch (Exception $e) {
        Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
        }
        }

        $this->_redirect('*/*/index');
        }

 public function gridAction()
        {
        $this->loadLayout();
        $this->getResponse()->setBody(
        $this->getLayout()->createBlock('Foo/adminhtml_Foo_grid')->toHtml()
        );
        }

/**
 * Check currently called action by permissions for current user
 *
 * @return bool
 */
protected function _isAllowed()
{
    return Mage::getSingleton('admin/session')->isAllowed('sales/foo_bar_baz');
}
}

here is config.xml file:

1.0.0

<global>
    <helpers>
        <foo_bar>
            <!-- This is where we define our helper directory -->
            <class>Foo_Bar_Helper</class>
        </foo_bar>
    </helpers>

    <blocks>
        <foo_bar>
            <!-- Set a block definition and lookup directory -->
            <class>Foo_Bar_Block</class>
        </foo_bar>
    </blocks>

    <models>
        <foo_bar>
            <!-- This is where we define our model directory -->
            <class>Foo_Bar_Model</class>
            <!-- Define a resource to create a custom table -->
            <resourceModel>foo_bar_mysql4</resourceModel>
        </foo_bar>

        <!-- Here's our resource model we'll use to create a database table -->
        <foo_bar_mysql4>
            <class>Foo_Bar_Model_Mysql4</class>
            <entities>
                <!-- Let's define our table, we'll call it with the baz name, but the real table is foo_bar_baz -->
                <!-- After we define our entity, we can call it with our model by calling foo_bar/baz -->
                <baz>
                    <table>foo_bar_baz</table>
                </baz>
            </entities>
        </foo_bar_mysql4>
    </models>

    <!-- And finally we define our resource setup script -->
    <resources>
        <foo_bar_setup>
            <setup>
                <module>Foo_Bar</module>
            </setup>
        </foo_bar_setup>
    </resources>
</global>

<admin>
    <routers>
        <adminhtml>
            <args>
                <!-- This is how we load our Adminhtml controllers -->
                <modules>
                    <Foo_Bar before="Mage_Adminhtml">Foo_Bar_Adminhtml</Foo_Bar>
                </modules>
            </args>
        </adminhtml>
    </routers>
</admin>

<adminhtml>
    <layout>
        <updates>
            <foo_bar>
                <!--
                We again keep a nice naming convention and make our module upgrade proof by placing it in a separate folder
                - Since we are in the adminhtml node, this will look for the XML file in the app/design/adminhtml/default/default root folder
                -->
                <file>foo/bar.xml</file>
            </foo_bar>
        </updates>
    </layout>
</adminhtml>

enter image description here

Best Answer

Your problem is here:


public function gridAction()
        {
        $this->loadLayout();
        $this->getResponse()->setBody(
        $this->getLayout()->createBlock('foo_bar/adminhtml_foo_grid')->toHtml()
        );
        }

'foo_bar/adminhtml_foo_grid' here

  1. foo_bar is your confix.xml block name
  2. adminhtml_foo_grid is the path of your Grid.php that means Vendor/Module/Block/Adminhtml/Foo/Grid.php
Related Topic