2

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

  • show your config.xml – Sohel Rana Jun 20 '16 at 6:20
6

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
2

We need config.xml file of your module in order to properly help you out. However we can give some hints based on the controller file that you have provided.

Why Error Happends ?

The error is producing by this line which you can see in Foo_Bar_Adminhtml_BazController::gridAction()

$this->getLayout()->createBlock('Foo/adminhtml_Foo_grid')->toHtml()

Here createBlock() function is used to create a new block in your layout. Reference to the block that you want to create is wrong and hence you are getting this error.

Story Behind Error

In general createBlock() has following structure:

 createBlock('block_group_name/path_to_your_file')
  • block_group_name stands for block unique reference in your module. In this case, you specified it as Foo. This is defining inside config.xml.

  • In the general context Magento will look for block a class Namespace_Module_Block_Path_To_Your_File in app\code\{local|community}\Namespace\Module\Block\Path\To\Your\File.php (Here I assume block_group_name is defined by the module Namespace_Module). So it is very important to have the class name and file path follows same concept in your case.

What You Should Check

So possible errors are :

  1. Your block group name reference may be wrong. ie Foo is wrong.
  2. Your block file path should be app\code\community\Foo\Bar\Block\Adminhtml\Foo\Grid.php
  3. Class name should be Foo_Bar_Block_Adminhtml_Foo_Grid.

After recheck all these points, please make sure you cleared all cache.

EDIT

I can see your config.xml file now. Based on this, the right code in your controller would be

$this->getLayout()->createBlock('foo_bar/adminhtml_foo_grid')->toHtml()
  • I have put this code in Controller.php file but don't solve my problem. – Rakesh Patidar Jun 20 '16 at 6:51
  • @RakeshPatidar did you check other check items ? 1,2, 3 ? – Rajeev K Tomy Jun 20 '16 at 6:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.