自定义模块网格容器不会显示

Custom module grid container will not show

我知道这个问题已经在 SO 上被问了很多次,但没有任何帮助我解决我的问题。

我试图在我的模块索引页面上显示一个网格,但它没有显示,我试图在一个循环中 var_dump Mage::getModel('custombundle/bundle')->getCollection(),它给了我数据输出。以下是我目前编写的代码:

config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Company_CustomBundle>
            <version>1.0.1</version>
        </Company_CustomBundle>
    </modules>
    <admin>
        <routers>
            <custombundle>
                <use>admin</use>
                <args>
                    <module>Company_CustomBundle</module>
                    <frontName>admin_custombundle</frontName>
                </args>
            </custombundle>
        </routers>
    </admin>

    <global>
        <helpers>
            <custombundle>
                <class>Company_CustomBundle_Helper</class>
            </custombundle>
        </helpers>

        <!-- Blocks -->
        <blocks>
            <company_custombundle>
                <class>Company_CustomBundle_Block</class>
            </company_custombundle>
        </blocks>

        <models>
            <custombundle>
                <class>Company_CustomBundle_Model</class>
                <resourceModel>custombundle_resource</resourceModel>
            </custombundle>
            <custombundle_resource>

                <class>Company_CustomBundle_Model_Resource</class>

                <entities>
                    <basket>
                        <table>custombundle_basket</table>
                    </basket>
                    <bundle>
                        <table>custombundle_bundle</table>
                    </bundle>
                </entities>

            </custombundle_resource>
        </models>

        <resources>

            <custombundle_setup>
                <setup>
                    <module>Company_CustomBundle</module>
                    <class>Company_CustomBundle_Model_Resource_Setup</class>
                </setup>
            </custombundle_setup>

            <custombundle_write>
                <connection>
                    <use>core_write</use>
                </connection>
            </custombundle_write>

            <custombundle_read>
                <connection>
                    <use>core_read</use>
                </connection>
            </custombundle_read>

        </resources>

    </global>

    <adminhtml>

        <!-- Layouts Configuration Starts -->
        <layout>
            <updates>
                <custombundle>
                    <file>custombundle.xml</file>
                </custombundle>
            </updates>
        </layout>
        <!-- !! Layouts Configuration -->

        <menu>
            <custombundle module="custombundle">
                <title>Custom Bundle</title>
                <sort_order>100</sort_order>
                <children>
                    <index module="custombundle">
                        <title>Custom Bundle</title>
                        <sort_order>0</sort_order>
                        <action>admin_custombundle/adminhtml_custombundle</action>
                    </index>

                    <other module="custombundle">
                        <title>Other</title>
                        <sort_order>0</sort_order>
                        <action>admin_custombundle/adminhtml_custombundle/other</action>
                    </other>
                </children>
            </custombundle>
        </menu>
    </adminhtml>

</config>

app/design/adminhtml/default/default/layout/custombundle.xml

<?xml version="1.0"?>

<layout version="0.1.0">

    <adminhtml_custombundle_index> <!-- custombundle controller index action -->
        <reference name="content">
            <block type="company_custombundle/adminhtml_custombundle_bundle" name="list_combination" />
        </reference>
    </adminhtml_custombundle_index>
</layout>

controllers/Adminhtml/CustombundleController.php

public function indexAction()
{
    $this->_title($this->__('Custom Bundle'))->_title($this->__('Category Combinations'))->loadLayout()->_setActiveMenu('custombundle/index');

    $this->renderLayout();
}

Block/Adminhtml/Custombundle/Bundle.php

class Company_CustomBundle_Block_Adminhtml_Custombundle_Bundle extends Mage_Adminhtml_Block_Widget_Grid_Container
{
    public function __construct()
    {
        $this->_controller = 'adminhtml_custombundle';
        $this->_blockGroup = 'company_custombundle';
        $this->_headerText = Mage::helper('company_custombundle')->__('Custom Bundle Category combinations');
        $this->_addButtonLabel = Mage::helper('company_custombundle')->__('Add Item');
        parent::__construct();
    }
}

Block/Adminhtml/Custombundle/Bundle/Grid.php

class Company_CustomBundle_Block_Adminhtml_Custombundle_Bundle_Grid extends Mage_Adminhtml_Block_Widget_Grid
{
    public function __construct()
    {
        parent::__construct();
        $this->setId('bundleGrid');
        $this->setDefaultSort('bundle_id');
        $this->setDefaultDir('DESC');
        $this->setSaveParametersInSession(true);
    }

    protected function _prepareCollection()
    {
        $collection = Mage::getModel('custombundle/bundle')->getCollection();
        $this->setCollection($collection);

        return parent::_prepareCollection();
    }

    protected function _prepareColumns()
    {
        $this->addColumn('bundle_id', array(
                'header' => 'ID',
                'align' => 'right',
                'width' => '50px',
                'index' => 'bundle_id',
        ));

        $this->addColumn('assigned_category_id', array(
              'header' => 'Assigned with',
              'align' => 'left',
              'index' => 'assigned_category_id',
        ));

        $this->addColumn('category_id', array(
              'header' => 'Category',
              'align' => 'left',
              'index' => 'category_id',
        ));

        return parent::_prepareColumns();
    }

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

您的管理路由未正确定义。您永远不应该添加新的管理路由,因为它会产生安全问题并且非常不鼓励。

<admin>
    <routers>
        <adminhtml>
            <args>
                <modules>
                    <Company_CustomBundle after="Mage_Adminhtml">Company_CustomBundle_Adminhtml</Company_CustomBundle>
                </modules>
            </args>
        </adminhtml>
    </routers>
</admin>

可以在此处找到有关管理路由问题的更多信息:https://magento.com/security/patches/supee-6788-technical-details

祝你好运

Rajeev and Jaimin you can check the answer as well as discussion here 在 Magento stackexchange 上正确回答了这个问题。这些人确实提供了宝贵的意见并提出了提高代码质量的改进建议。

我将尝试解释一下我在代码中所做的解决问题的更正。

config.xml

<admin>
    <routers>
        <adminhtml>
            <args>
                <modules>
                    <company_custombundle before="Mage_Adminhtml">Company_CustomBundle_Adminhtml</company_custombundle>
                </modules>
            </args>
        </adminhtml>
    </routers>
</admin>

在 config.xml 部分我更改了 router 部分,因为 <use>admin</use> 从 1.9 版本开始已被弃用,并且如 所述具有潜在的安全威胁。

然后相应地更正了config.xml的菜单部分以支持新的路由如下:

<menu>
    <custombundle module="custombundle">
        <title>Custom Bundle</title>
        <sort_order>100</sort_order>
        <children>
            <index module="custombundle">
                <title>Custom Bundle</title>
                <sort_order>0</sort_order>
                <action>adminhtml/custombundle/index</action>
            </index>

            <other module="custombundle">
                <title>Other</title>
                <sort_order>0</sort_order>
                <action>adminhtml/custombundle/other</action>
            </other>
        </children>
    </custombundle>
</menu>

Block/Adminhtml/Custombundle/Bundle.php

class Company_CustomBundle_Block_Adminhtml_Custombundle_Bundle extends Mage_Adminhtml_Block_Widget_Grid_Container
{
    public function __construct()
    {
        $this->_controller = 'adminhtml_custombundle_bundle';
        $this->_blockGroup = 'company_custombundle';
        $this->_headerText = Mage::helper('custombundle')->__('Custom Bundle Category combinations');
        $this->_addButtonLabel = Mage::helper('custombundle')->__('Add Item');
        parent::__construct();
    }
}

现在,我将 adminhtml_custombundle 更改为 adminhtml_custombundle_bundle 完成此更改是因为如果您查看 Mage_Adminhtml_Block_Widget_Container::_prepareLayout() 方法,您将了解容器的子项,即 grid或者如果您不这样做,将永远不会调用 Block/Adminhtml/Custombundle/Bundle/Grid.php

上述代码的另一个变化是 Mage::Helper() 我将它的值从 company_custombundle 更改为 custombundle 只是没有命名空间。

毕竟我清除了 magento 的缓存并重新加载... *Bingo 网格在那里显示数据。