【问题标题】:Invalid block type - Custom Grid - Magento Admin Panel无效的块类型 - 自定义网格 - Magento 管理面板
【发布时间】:2016-03-16 09:29:15
【问题描述】:

我制作了一个模块并且也已注册.. 在我想创建一个自定义网格之前它工作得很好...我在必要的文件中编写了以下代码,但是抛出了一个异常,我有无效的块指定的类型。我已经经历了很多次,但我无法弄清楚错误。

/home/vhost/_default/magento1.9.2/app/code/local/Maven/Questionanswer/Block/Adminhtml/Questionanswer

  <?php

    class Maven_Questionanswer_Block_Adminhtml_Questionanswer_Question extends Mage_Adminhtml_Block_Widget_Grid_Container
    {
        public function __construct()
        {
            $this->_blockGroup = 'questionanswer';
            $this->_controller = 'adminhtml_question';
            $this->_headerText =  Mage::helper('questionanswer')->__('questionanswer');
            $this->_addButtonLabel = 'Add Question';
            parent::__construct();
        }
    }
?>

/home/vhost/_default/magento1.9.2/app/code/local/Maven/Questionanswer/Block/Adminhtml/Questionanswer/Questionanswer

  <?php

    class Maven_Questionanswer_Block_Adminhtml_Questionanswer_Questionanswer_Grid extends Mage_Adminhtml_Block_Widget_Grid
    {
        public function __construct()
       {
           parent::__construct();
           $this->setId('questionGrid');
           $this->setDefaultSort('question_id');
           $this->setDefaultDir('DESC');
           $this->setSaveParametersInSession(true);
       }
        protected function _prepareCollection()
        {
            $collection = Mage::getModel('questionanswer/question')->getCollection();
            $this->setCollection($collection);
            parent::_prepareCollection();
            return $this;
        }

        protected function _prepareColumns()
        {
            $helper = Mage::helper('questionanswer');

            $this->addColumn('question_id',
                 array(
                        'header' => 'ID',
                        'align' =>'right',
                        'width' => '50px',
                        'index' => 'id_pfay_test',
                   ));
           $this->addColumn('username',
                   array(
                        'header' => 'Username',
                        'align' =>'left',
                        'index' => 'username',
                  ));
           $this->addColumn('email', array(
                        'header' => 'email',
                        'align' =>'left',
                        'index' => 'email',
                 ));
            $this->addColumn('question', array(
                         'header' => 'question',
                         'align' =>'left',
                         'index' => 'question',
              ));
            $this->addColumn('status', array(
                         'header' => 'status',
                         'align' =>'left',
                         'index' => 'status',
              ));
             return parent::_prepareColumns();
         }

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

**/home/vhost/_default/magento1.9.2/app/code/local/Maven/Questionanswer/controllers/Adminhtml**

    <?php

    class Maven_Questionanswer_Adminhtml_QuestionController extends Mage_Adminhtml_Controller_Action
    {


        public function indexAction()
        {
            $this->_title($this->__('Question'))->_title($this->__('Question Answer'));
            $this->loadLayout();
            $this->_setActiveMenu('questionanswer/question');
            $this->_addContent($this->getLayout()->createBlock('maven_questionanswer/adminhtml_questionanswer_question'));
            $this->renderLayout();
        }

        public function gridAction()
        {
            $this->loadLayout();
            $this->getResponse()->setBody(
            $this->getLayout()->createBlock('maven_questionanswer/adminhtml_questionanswer_questionanswer_grid')->toHtml());

        }

        // public function exportInchooCsvAction()
        // {
        //     $fileName = 'orders_inchoo.csv';
        //     $grid = $this->getLayout()->createBlock('inchoo_orders/adminhtml_sales_order_grid');
        //     $this->_prepareDownloadResponse($fileName, $grid->getCsvFile());
        // }

        // public function exportInchooExcelAction()
        // {
        //     $fileName = 'orders_inchoo.xml';
        //     $grid = $this->getLayout()->createBlock('inchoo_orders/adminhtml_sales_order_grid');
        //     $this->_prepareDownloadResponse($fileName, $grid->getExcelFile($fileName));
        // }


    }
?>

/home/vhost/_default/magento1.9.2/app/code/local/Maven/Questionanswer/etc/config.xml

  <blocks>
                <questionanswer>
                    <class>Maven_Questionanswer_Block</class>
                </questionanswer>
            </blocks>

    <admin>
            <routers>
                <adminhtml>
                    <use>admin</use>
                    <args>
                        <modules>
                            <maven_questionanswer>Maven_Questionanswer_Adminhtml</maven_questionanswer>
                        </modules>
                    </args>
                </adminhtml>
            </routers>
        </admin>

and in adminhtml.xml

<?xml version="1.0" encoding="UTF-8"?>
    <adminhtml>
        <menu>
            <questionanswer translate="title" module="questionanswer">
                <title>Question Answer</title>
                <sort_order>60</sort_order>
                <children>
                     <questionanswer_question module="questionanswer">
                        <title>Question</title> 
                        <action>adminhtml/question</action>
                     </questionanswer_question> 
                </children>      
            </questionanswer> 
        </menu>
        <acl>
            <resources>
                <all>
                    <title>Allow Everything</title>
                </all>
                <admin>
                    <children>
                        <system>
                            <children>
                                <config>
                                    <children>
                                        <questionanswer_section>
                                            <title>Question-Answer</title>
                                        </questionanswer_section>
                                    </children>
                                </config>
                            </children>
                        </system>
                    </children>
                </admin>
            </resources>
        </acl>
    </adminhtml>

我还创建了一个空白助手...我哪里出错了?

【问题讨论】:

    标签: php magento model-view-controller


    【解决方案1】:

    根据Grid 块,您的目录结构似乎不正确。 请按照以下步骤解决您的问题:

    第 1 步:从以下位置重新定位您的 Grid.php 文件:

    /home/vhost/_default/magento1.9.2/app/code/local/Maven/Questionanswer/Block/Adminhtml/Questionanswer/Questionanswer

    /home/vhost/_default/magento1.9.2/app/code/local/Maven/Questionanswer/Block/Adminhtml/Questionanswer/

    第 2 步:更改您的网格类名称:

    Maven_Questionanswer_Block_Adminhtml_Questionanswer_Questionanswer_Grid

    Maven_Questionanswer_Block_Adminhtml_Questionanswer_Grid

    第 3 步:清除 Magento 缓存并尝试运行加载网格页面。


    更新:

    请更改:

    $this->getLayout()->createBlock('maven_questionanswer/adminhtml_questionanswer_questionanswer_grid')->toHtml());

    $this->getLayout()->createBlock('questionanswer/adminhtml_questionanswer_questionanswer_grid')->toHtml());

    当您将块声明为questionanswer 而不是maven_questionanswer

    【讨论】:

    • 感谢您的回复。我试图改变类规范,我也改变了网格的目录结构。但仍然是同样的错误.. 1)我把 Grid.php 带到一个目录向上。我将 Question.php 更改为目录 /block/adminhtml/Question.php 2)我还在 createBlock() 函数中更改了控制器中的目录规范...仍然是相同的错误
    • 您能否验证this blog 中提到的您为自定义网格所做的所有过程?
    • inchoo.net/magento/how-to-create-a-custom-grid-from-scratch 我厌倦了这个网站上写的所有步骤.. 与指定的相同
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多