我想创建我自己的管理网站。

配置.xml:

<config>
    <modules>
        <Mymodule_Test>
            <version>0.1.0</version>
        </Mymodule_Test>
    </modules>
    <global>
        <helpers>
            <test>
                <class>Mymodule_Test_Helper</class>
            </test>
        </helpers>
    </global>
    <admin>
        <routers>
            <mymoduletest>
                <use>admin</use>
                <args>
                   <module>Mymodule_Test</module>
                   <frontName>mymoduletest</frontName>
                </args>
            </mymoduletest>
        </routers>
    </admin>
    <adminhtml>
        <layout>
            <updates>
                <test>
                    <file>test.xml</file>
                </test>
            </updates>
        </layout>
    </adminhtml>
</config>

管理html.xml:

<menu>
    <test translate="title" module="test">
        <title>Test</title>
        <sort_order>300</sort_order>
        <children>
            <!-- child items go here -->
            <show translate="title" module="test">
                <title>SubTest</title>
                <sort_order>10</sort_order>
                <action>mymoduletest/adminhtml_show</action>
            </show>
        </children>
    </test>
</menu>

控制器\Adminhtml\ShowController.php :

<?php Class Mymodule_Test_Adminhtml_ShowController extends Mage_Adminhtml_Controller_Action{

protected function _initAction()
{
    $this->loadLayout();
    $this->_setActiveMenu('test/show');
    return $this;
}

public function indexAction()
{
    $this->_initAction()
         ->_addContent($this->getLayout()->createBlock('Mymodule_Test_Block_Adminhtml_Show'))
         ->renderLayout();
}}

块\Adminhtml\Show.php :

<?php class Mymodule_Test_Block_Adminhtml_Show extends Mage_Adminhtml_Block_Widget_Form_Container{

public function __construct()
{

    parent::__construct();

    $this->removeButton('back');
    $this->removeButton('reset');
    $this->removeButton('delete');
    $this->removeButton('add');
    $this->removeButton('save');
    $this->removeButton('edit');

    $this->setId('testContainer');

    $this->_blockGroup = 'test';
    $this->_controller = 'adminhtml';
    $this->_mode = 'show';

    $this->_headerText  = 'TEST';
}}

我的管理页面显示了 headText,但我得到了这个异常:

异常“Mage_Core_Exception”,消息“无效的块类型:”mage_test_block_adminhtml_show_form'in/var/www/html/magento/app/mage.php:595

在我的管理页面上:

致命错误:致电布尔值的成员函数setData()在/var/www/html/magento/magento/app/code/core/mage/mage/adminhtml/block/block/widget/form/form/container.php on Line 129上

为什么magento创建块: Mage_Test_Block_Adminhtml_Show_Form 并不是 Mymodule_Test_Block_Adminhtml_Show_Form ???

Block\Adminhtml\Show\Form.php :

<?php class Mymodule_Test_Block_Adminhtml_Show_Form extends Mage_Adminhtml_Block_Widget_Form {

public function __construct()
{
    parent::__construct();

    $this->setId('showGeneralForm');
    $this->setTemplate('test/show.phtml');
}

protected function _beforeToHtml()
{
    return parent::_beforeToHtml();
}}
有帮助吗?

解决方案

<global>标记中的config.xml文件中添加以下内容

<blocks>
    <test>
      <class>Mymodule_Test_Block</class>
   </test>
</blocks>
.

其他提示

这应该是这样的:

在config.xml中

<config>
.....
    <global>
       .......
       <blocks>
            <test>
               <class>Mymodule_Test_Block</class>
            </test>
       </blocks>
      .......
    </global>
.....
</config>
.

和在你的控制器中

public function indexAction()
{
    $this->_initAction()
         ->_addContent($this->getLayout()->createBlock('test/adminhtml_show'))
         ->renderLayout();
}
.

在你的 config.xml 文件你没有定义类

但是你已经调用了块类 Mymodule_Test_Block_Adminhtml_ShowShowController.php

根据magento系统,你 不能使用直接类 名字上 创建块 功能。

需要通过块类型调用块类

所以,需要在xml中定义块类型

<global>
        <blocks>
            <test> <-- module block identifire -->
                <class>Mymodule_Test_Block</class>
            </test>
        </blocks>

        <helpers>
    ...

当你想打电话给班级时 Mymodule_Test_Block_Adminhtml_Show 然后

它的块类型应该是 'test/adminhtml_show'

根据magento系统

  • test => Mymodule_Test_Block
  • adminhtml_show =>Adminhtml_Show

$this->getLayout()->createBlock('Mymodule_Test_Block_Adminhtml_Show')

改成

$this->getLayout()->createBlock('test/adminhtml_show')
许可以下: CC-BY-SA归因
scroll top