Magento Admin网格未加载

时间:2016-07-18 06:42:41

标签: php magento

我正在开发一个模块。网格块未加载并抛出异常。

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}

配置文件是

<?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>

索引控制器位于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');
    }

}

网格容器位于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();
}
}

网格小部件位于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()));
    }
}

任何人都可以帮助我吗?

2 个答案:

答案 0 :(得分:0)

将您的网格文件重命名为Mygrid.php(第一个字母应为大写字母),并在config.xml文件中将管理路由器部分更改为:

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

问题是您正在使用adminhtml部分的前端路由器声明。您应该以不同方式声明adminhtml的路由器。

答案 1 :(得分:0)

请参阅以下需要进行的更改。类名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()));
    }
}

在您的控制器中,索引操作更改

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

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

在您的config.xm l档文件中看到您的阻止论坛群组名称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');
    }

}