简体   繁体   English

Magento Admin网格未加载

[英]Magento Admin grid not loading

I am developing a module. 我正在开发一个模块。 The grid block is not loading and throws exception. 网格块未加载,并引发异常。

2016-07-18T06:11:29+00:00 ERR (3): 
exception 'Mage_Core_Exception' with message 'Invalid block type: Mage_Somdeb_Security_Block_Adminhtml_Mygrid' in /chroot/home/crossdre/staging.crossdresser.com/html/app/Mage.php:595
Stack trace:
#0 /chroot/home/crossdre/staging.crossdresser.com/html/app/code/core/Mage/Core/Model/Layout.php(495): Mage::throwException('Invalid block t...')
#1 /chroot/home/crossdre/staging.crossdresser.com/html/app/code/core/Mage/Core/Model/Layout.php(437): Mage_Core_Model_Layout->_getBlockInstance('somdeb_security...', Array)
#2 /chroot/home/crossdre/staging.crossdresser.com/html/app/code/local/Somdeb/Security/controllers/Adminhtml/IndexController.php(16): Mage_Core_Model_Layout->createBlock('somdeb_security...')
#3 /chroot/home/crossdre/staging.crossdresser.com/html/app/code/core/Mage/Core/Controller/Varien/Action.php(418): Somdeb_Security_Adminhtml_IndexController->indexAction()
#4 /chroot/home/crossdre/staging.crossdresser.com/html/app/code/core/Mage/Core/Controller/Varien/Router/Standard.php(254): Mage_Core_Controller_Varien_Action->dispatch('index')
#5 /chroot/home/crossdre/staging.crossdresser.com/html/app/code/core/Mage/Core/Controller/Varien/Front.php(172): Mage_Core_Controller_Varien_Router_Standard->match(Object(Mage_Core_Controller_Request_Http))
#6 /chroot/home/crossdre/staging.crossdresser.com/html/app/code/core/Mage/Core/Model/App.php(354): Mage_Core_Controller_Varien_Front->dispatch()
#7 /chroot/home/crossdre/staging.crossdresser.com/html/app/Mage.php(684): Mage_Core_Model_App->run(Array)
#8 /chroot/home/crossdre/staging.crossdresser.com/html/index.php(89): Mage::run('', 'store')
#9 {main}

The config file is 配置文件是

<?xml version="1.0"?>
<config>
  <modules>
    <Somdeb_Security>
      <version>0.1.0</version>
    </Somdeb_Security>
  </modules>
  <frontend>
    <routers>
      <security>
        <use>standard</use>
        <args>
          <module>Somdeb_Security</module>
          <frontName>security</frontName>
        </args>
      </security>
    </routers>
  </frontend>
<admin>
  <routers>
    <security>
     <use>admin</use>
     <args>
      <module>Somdeb_Security</module>
      <frontName>admin_security</frontName>
     </args>
    </security>
  </routers>
</admin>
<global>
  <helpers>
    <security>
      <class>Somdeb_Security_Helper</class>
    </security>
  </helpers>
  <blocks>
       <security>
                <!-- Set a block definition and lookup directory -->
        <class>Somdeb_Security_Block</class>
       </security>
        </blocks>

        <models>
            <security>
                <!-- This is where we define our model directory -->
                <class>somdeb_security_Model</class>
                <!-- Define a resource to create a custom table -->
                <resourceModel>somdeb_security_mysql4</resourceModel>
            </security>

            <!-- Here's our resource model we'll use to create a database table -->
            <security_mysql4>
                <class>Somdeb_Security_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>somdeb_securtity_baz</table>
                    </baz>
                </entities>
            </security_mysql4>
        </models>
        <resources>
            <somdeb_security_setup>
                <setup>
                    <module>Somdeb_Security</module>
                </setup>
            </somdeb_security_setup>
        </resources>
</global>
<adminhtml>
  <layout>
      <updates>
          <Security>
              <file>somdeb/security.xml</file>
           </Security>
      </updates>
   </layout>   
 <menu>
   <security module="security">
     <title>Security</title>
     <sort_order>100</sort_order>
    <children>
      <security module="security">
        <title>Security </title>
        <sort_order>0</sort_order>
        <action>admin_security/adminhtml_index</action>
      </security>
    </children>
   </security>
 </menu>
</adminhtml>

</config>

The Index Controller is located in html/app/code/local/Somdeb/Security/controllers/IndexController.php 索引控制器位于html/app/code/local/Somdeb/Security/controllers/IndexController.php

<?php

class Somdeb_Security_Adminhtml_IndexController extends Mage_Adminhtml_Controller_Action {

  /**
  * Admin controller index action
  *
  * @access public
  * @return void
  */

  public function indexAction()
    {
         $this->loadLayout();
         //$this->_setActiveMenu('sales/sales');
        $this->_addContent($this->getLayout()->createBlock('somdeb_security/adminhtml_mygrid'));
        //$myblock = $this->getLayout()->createBlock('security/adminhtml_mygrid');
        //print_r($myblock);
   // $this->_addContent($myblock);  
        // Let's call our initAction method which will set some basic params for each action
        $this->renderLayout();
            print_r(Mage::getSingleton('core/layout')->getUpdate()->getHandles());
//die();
    }  

    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('Somdeb_Security/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 Baz'));

        $data = Mage::getSingleton('adminhtml/session')->getBazData(true);
        if (!empty($data)) {
            $model->setData($data);
        }  

        Mage::register('foo_bar', $model);

        $this->_initAction()
            ->_addBreadcrumb($id ? $this->__('Edit Baz') : $this->__('New Baz'), $id ? $this->__('Edit Baz') : $this->__('New Baz'))
            ->_addContent($this->getLayout()->createBlock('foo_bar/adminhtml_baz_edit')->setData('action', $this->getUrl('*/*/save')))
            ->renderLayout();
    }

    public function saveAction()
    {
        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('Somdeb_Security/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('sales/Somdeb_Security_baz')
            ->_title($this->__('Sales'))->_title($this->__('Baz'))
            ->_addBreadcrumb($this->__('Sales'), $this->__('Sales'))
            ->_addBreadcrumb($this->__('Baz'), $this->__('Baz'));

        return $this;
    }

    /**
     * Check currently called action by permissions for current user
     *
     * @return bool
     */
    protected function _isAllowed()
    {
        return Mage::getSingleton('admin/session')->isAllowed('sales/Somdeb_Security_baz');
    }

}

The Grid container is located in html/app/code/local/Somdeb/Security/Block/Adminhtml/mygrid.php 网格容器位于html/app/code/local/Somdeb/Security/Block/Adminhtml/mygrid.php

<?php
class Somdeb_Security_Block_Adminhtml_Mygrid extends Mage_Adminhtml_Block_Widget_Grid_Container
{
    public function __construct()
    {
        // The blockGroup must match the first half of how we call the block, and controller matches the second half
        // ie. foo_bar/adminhtml_baz
        echo "fffffffffffffff";
        $this->_blockGroup = 'security';
        $this->_controller = 'adminhtml_security';
        $this->_headerText = $this->__('Baz');

        parent::__construct();
    }
    protected function _prepareLayout()
{
    $this->setChild( 'grid',
        $this->getLayout()->createBlock( $this->_blockGroup.'/' . $this->_controller . '_grid',
        $this->_controller . '.grid')->setSaveParametersInSession(true) );
    return parent::_prepareLayout();
}
}

The grid widget is located in html/app/code/local/Somdeb/Security/Block/Adminhtml/security/Grid.php 网格小部件位于html/app/code/local/Somdeb/Security/Block/Adminhtml/security/Grid.php

<?php
class Somdeb_Security_Adminhtml_Security extends Mage_Adminhtml_Block_Widget_Grid
{
   public function __construct()
    {
        parent::__construct();

        // Set some defaults for our grid
        $this->setDefaultSort('id');
        $this->setId('somdeb_security_baz_grid');
        $this->setDefaultDir('asc');
        $this->setSaveParametersInSession(true);
    }

    protected function _getCollectionClass()
    {
        // This is the model we are using for the grid
        return 'Somdeb_Security/baz_collection';
    }

    protected function _prepareCollection()
    {
        // Get and set our collection for the grid
        $collection = Mage::getResourceModel($this->_getCollectionClass());
        $this->setCollection($collection);

        return parent::_prepareCollection();
    }

    protected function _prepareColumns()
    {
        // Add the columns that should appear in the grid
        $this->addColumn('id',
            array(
                'header'=> $this->__('ID'),
                'align' =>'right',
                'width' => '50px',
                'index' => 'id'
            )
        );

        $this->addColumn('name',
            array(
                'header'=> $this->__('Name'),
                'index' => 'name'
            )
        );

        return parent::_prepareColumns();
    }

    public function getRowUrl($row)
    {
        // This is where our row data will link to
        return $this->getUrl('*/*/edit', array('id' => $row->getId()));
    }
}

Can anyone help me ? 谁能帮我 ?

Rename your grid file to Mygrid.php (the first letter should be upper-cased), and inside the config.xml file change the admin router section as: 将网格文件重命名为Mygrid.php (首字母应大写),然后在config.xml文件中将“管理路由器”部分更改为:

<admin>
    <routers>
        <adminhtml>
            <args>
                <modules>
                    <security before="Mage_Adminhtml">Somdeb_Security_Adminhtml</security>
                </modules>
            </args>
        </adminhtml>            
    </routers>
</admin>

The issue is that you are using the frontend router declaration for the adminhtml section too. 问题是您也在adminhtml部分中使用了前端路由器声明。 You should declare the router for adminhtml differently. 您应该为adminhtml声明路由器。

Please see the following changes which you need to make. 请查看您需要进行的以下更改。 Grid is missing in class name app/code/local/Somdeb/Security/Block/Adminhtml/Security/Grid.php 类名app/code/local/Somdeb/Security/Block/Adminhtml/Security/Grid.php

<?php
class Somdeb_Security_Block_Adminhtml_Security_Grid extends Mage_Adminhtml_Block_Widget_Grid
{
   public function __construct()
    {
        parent::__construct();

        // Set some defaults for our grid
        $this->setDefaultSort('id');
        $this->setId('somdeb_security_baz_grid');
        $this->setDefaultDir('asc');
        $this->setSaveParametersInSession(true);
    }

    protected function _getCollectionClass()
    {
        // This is the model we are using for the grid
        return 'Somdeb_Security/baz_collection';
    }

    protected function _prepareCollection()
    {
        // Get and set our collection for the grid
        $collection = Mage::getResourceModel($this->_getCollectionClass());
        $this->setCollection($collection);

        return parent::_prepareCollection();
    }

    protected function _prepareColumns()
    {
        // Add the columns that should appear in the grid
        $this->addColumn('id',
            array(
                'header'=> $this->__('ID'),
                'align' =>'right',
                'width' => '50px',
                'index' => 'id'
            )
        );

        $this->addColumn('name',
            array(
                'header'=> $this->__('Name'),
                'index' => 'name'
            )
        );

        return parent::_prepareColumns();
    }

    public function getRowUrl($row)
    {
        // This is where our row data will link to
        return $this->getUrl('*/*/edit', array('id' => $row->getId()));
    }
}

In your controller, index action change 在您的控制器中,索引动作发生了变化

$this->_addContent($this->getLayout()->createBlock('somdeb_security/adminhtml_mygrid'));

to

$this->_addContent($this->getLayout()->createBlock('security/adminhtml_mygrid'));

See in your config.xm l file your block group name id security not somdeb_security 在您的config.xm l文件中看到您的块组名称id security不是somdeb_security

<?php

class Somdeb_Security_Adminhtml_IndexController extends Mage_Adminhtml_Controller_Action {

  /**
  * Admin controller index action
  *
  * @access public
  * @return void
  */

  public function indexAction()
    {
         $this->loadLayout();
         //$this->_setActiveMenu('sales/sales');
        $this->_addContent($this->getLayout()->createBlock('security/adminhtml_mygrid'));
        //$myblock = $this->getLayout()->createBlock('security/adminhtml_mygrid');
        //print_r($myblock);
   // $this->_addContent($myblock);  
        // Let's call our initAction method which will set some basic params for each action
        $this->renderLayout();
            print_r(Mage::getSingleton('core/layout')->getUpdate()->getHandles());
//die();
    }  

    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('Somdeb_Security/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 Baz'));

        $data = Mage::getSingleton('adminhtml/session')->getBazData(true);
        if (!empty($data)) {
            $model->setData($data);
        }  

        Mage::register('foo_bar', $model);

        $this->_initAction()
            ->_addBreadcrumb($id ? $this->__('Edit Baz') : $this->__('New Baz'), $id ? $this->__('Edit Baz') : $this->__('New Baz'))
            ->_addContent($this->getLayout()->createBlock('foo_bar/adminhtml_baz_edit')->setData('action', $this->getUrl('*/*/save')))
            ->renderLayout();
    }

    public function saveAction()
    {
        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('Somdeb_Security/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('sales/Somdeb_Security_baz')
            ->_title($this->__('Sales'))->_title($this->__('Baz'))
            ->_addBreadcrumb($this->__('Sales'), $this->__('Sales'))
            ->_addBreadcrumb($this->__('Baz'), $this->__('Baz'));

        return $this;
    }

    /**
     * Check currently called action by permissions for current user
     *
     * @return bool
     */
    protected function _isAllowed()
    {
        return Mage::getSingleton('admin/session')->isAllowed('sales/Somdeb_Security_baz');
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM