Admin Grid View Not Working in Magento – Custom Module Fix

magento-1.7magento-1.8magento-1.9php-5.4

I am new to magento and try to write a magento admin module.i followed this Custom Module with Custom Database Table

Hope i followed every steps but i can't view my menu even.Sql setup also not run and create the table,I cleared the cache. Magento version is 1.9.

Config.xml : path(app\code\local\Ffriends\Pets\etc)

<?xml version="1.0" encoding="utf-8"?>
<config>
<modules>
    <Ffriends_Pets>
        <version>0.1.0<</version>
    </Ffriends_Pets>
</modules>
<frontend>
    <routers>
        <pets>
            <use>standard</use>
            <args>
                <module>Ffriends_Pets</module>
                <frontName>pets</frontName>
            </args>
        </pets>
    </routers>
    <layout>
        <updates>
            <pets>
                <file>pets.xml</file>
            </pets>
        </updates>
    </layout>
</frontend>
<global>
    <helpers>
        <pets>
            <class>Ffriends_Pets_Helper</class>
        </pets>
    </helpers>
    <models>
        <pets>
            <class>Ffriends_Pets_Model</class>
            <resourceModel>pets_mysql4</resourceModel>
        </pets>
        <pets_mysql4>
            <class>Ffriends_Pets_Model_Mysql4</class>
            <entities>
                <pets>
                    <table>pets</table>
                </pets>
            </entities>
        </pets_mysql4>
    </models>
    <resources>
        <pets_setup>
            <setup>
                <module>Ffriends_Pets</module>
            </setup>
            <connection>
                <use>core_setup</use>
            </connection>
        </pets_setup>
        <pets_write>
            <connection>
                <use>core_write</use>
            </connection>
        </pets_write>
        <pets_read>
            <connection>
                <use>core_read</use>
            </connection>
        </pets_read>
    </resources>
    <blocks>
        <pets>
            <class>Ffriends_Pets_Block</class>
        </pets>
    </blocks>

</global>    
<admin>
    <routers>
        <pets>
            <use>admin</use>
            <args>
                <module>Ffriends_Pets</module>
                <frontName>pets</frontName>
            </args>
        </pets>
    </routers>
</admin>
<adminhtml>
    <menu>
        <pets module="pets">
            <title>Pets</title>
            <sort_order>200</sort_order>               
            <children>
                <items module="pets">
                    <title>Manage Items</title>
                    <sort_order>0</sort_order>
                    <action>pets/adminhtml_pets</action>
                </items>
            </children>
        </pets>
    </menu>
    <acl>
        <resources>
            <all>
                <title>Allow Everything</title>
            </all>
            <admin>
                <children>
                    <pets>
                        <title>Pets Module</title>
                        <sort_order>200</sort_order>
                    </pets>
                </children>
            </admin>
        </resources>   
    </acl>
    <layout>
        <updates>
            <pets>
                <file>pets.xml</file>
            </pets>
        </updates>
    </layout>
</adminhtml>
</config>

layout.xml file – Path(\app\design\adminhtml\default\default\layout).

<?xml version="1.0" encoding="utf-8"?>
<layout version="0.1.0">
<pets_adminhtml_pets_index>
    <reference name="content">
        <block type="pets/adminhtml_pets" name="pets" />
    </reference>
</pets_adminhtml_pets_index>
</layout>

Pets.php file – Path(app\code\local\Ffriends\Pets\Block\Adminhtml).

<?php
class Ffriends_Pets_Block_Adminhtml_Pets extends Mage_Adminhtml_Block_Widget_Grid_Container{
    public function __construct()
    {
        $this->_controller = 'adminhtml_pets';
        $this->_blockGroup = 'pets';
        $this->_headerText = Mage::helper('pets')->__('Item Manager');
        $this->_addButtonLabel = Mage::helper('pets')->__('Add Item');
        parent::__construct();
    }

}

Grid.php file – Path(app\code\local\Ffriends\Pets\Block\Adminhtml\Pets).

<?php
class Ffriends_Pets_Block_Adminhtml_Pets_Grid extends Mage_Adminhtml_Block_Widget_Grid
{
public function __construct()
{
    parent::__construct();
    $this->setId('petsGrid');
    // This is the primary key of the database
    $this->setDefaultSort('pets_id');
    $this->setDefaultDir('ASC');
    $this->setSaveParametersInSession(true);
    $this->setUseAjax(true);
}

protected function _prepareCollection()
{
    $collection = Mage::getModel('pets/pets')->getCollection();
    $this->setCollection($collection);
    return parent::_prepareCollection();
}

protected function _prepareColumns()
{
    $this->addColumn('pets_id', array(
        'header'    => Mage::helper('pets')->__('ID'),
        'align'     =>'right',
        'width'     => '50px',
        'index'     => 'pets_id',
    ));

    $this->addColumn('title', array(
        'header'    => Mage::helper('pets')->__('Title'),
        'align'     =>'left',
        'index'     => 'title',
    ));

    /*
    $this->addColumn('content', array(
        'header'    => Mage::helper('<module>')->__('Item Content'),
        'width'     => '150px',
        'index'     => 'content',
    ));
    */

    $this->addColumn('created_time', array(
        'header'    => Mage::helper('pets')->__('Creation Time'),
        'align'     => 'left',
        'width'     => '120px',
        'type'      => 'date',
        'default'   => '--',
        'index'     => 'created_time',
    ));

    $this->addColumn('update_time', array(
        'header'    => Mage::helper('pets')->__('Update Time'),
        'align'     => 'left',
        'width'     => '120px',
        'type'      => 'date',
        'default'   => '--',
        'index'     => 'update_time',
    ));   


    $this->addColumn('status', array(

        'header'    => Mage::helper('pets')->__('Status'),
        'align'     => 'left',
        'width'     => '80px',
        'index'     => 'status',
        'type'      => 'options',
        'options'   => array(
            1 => 'Active',
            0 => 'Inactive',
        ),
    ));

    return parent::_prepareColumns();
}

public function getRowUrl($row)
{
    return $this->getUrl('*/*/edit', array('id' => $row->getId()));
}

public function getGridUrl()
{
  return $this->getUrl('*/*/grid', array('_current'=>true));
}
}

Great pleasure if some one can help me to overcome this.Thank you.

Best Answer

I cant see any menu codes here. So provide us with adminhtml.xml if any.

However I will give you my random thoughts here. Your admin route is setup like this.

 <routers>
    <pets>
        <use>admin</use>
        <args>
            <module>Ffriends_Pets</module>
            <frontName>pets</frontName>
        </args>
    </pets>
</routers>

What this do is setuping an admin router for your module. It has no problem as of now. But this will leads all of your module's url (admin side) looks like this

  www.domain.com/pets/controller/method

But if you have a look on all of admin urls, it follows this convention

  www.domain.com/admin/controller/method

If you need the same effect, then you need to change your admin router setup like this.

<admin>
    <routers>
        <adminhtml>
            <args>
                <modules>
                    <ffriends_pets before="Mage_Adminhtml">Ffriends_Pets_Adminhtml</ffriends_pets >
                </modules>
            </args>
        </adminhtml>
    </routers>
</admin>

Second point comes in layout xml file. As per your current configuration, your layout xml file looks good. However if you adopt the method that I put forward, then there is slight change comes in layout xml file. Layout handle that you are currently using pets_adminhtml_pets_index will change to adminhtml_pets_index. This is because instead of pets router configuration now we are using adminhtml router configuration which is defined inside the core module Mage_Adminhtml.


Now as per the layout xml and configuration, there should be a controller present in your module. It would be

File : app/code/local/Ffriends/Pets/controllers/Adminhtml/PetsController.php

<?php
class Ffriends_Pets_Adminhtml_PetsController extends Mage_Adminhtml_Controller_Action
{
     public function indexAction()
     {
         $this->loadLayout();
         $this->renderLayout();
     }
}

Without this controller, nothing is going to show in admin side.

Side Note :- In order to have a custom menu, you need additional configurations. It will be normally do in adminhtml.xml file. There are plenty of tutorials available outside for set uping a menu. So google it.

Related Topic