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

时间:2017-02-02 12:24:53

标签: php magento magento-1.9

我知道这个问题在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>

应用/设计/ adminhtml /默认/默认/布局/ 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>

控制器/ Adminhtml / CustombundleController.php

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

    $this->renderLayout();
}

阻止/ 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();
    }
}

阻止/ Adminhtml / Custombundle /捆绑/ 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()));
    }
}

2 个答案:

答案 0 :(得分:1)

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

<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

祝你好运

答案 1 :(得分:0)

RajeevJaimin可以在Magento stackexchange上正确回答这个问题,您可以查看答案以及讨论here。这些人真的提供了宝贵的意见,并提出了改进代码质量的改进建议。

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

<强> 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版本开始被弃用,并且具有Mladen Ilić所述的潜在安全威胁。

然后相应地更正了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>

阻止/ 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()方法,您将会知道您的容器的孩子即{{1}如果您不这样做,我或永远不会调用grid

上述代码中的另一项更改是Block/Adminhtml/Custombundle/Bundle/Grid.php我将其值从Mage::Helper()更改为company_custombundle,即没有命名空间。

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