【问题标题】:Display template in Admin Module of MagentoMagento 管理模块中的显示模板
【发布时间】:2016-05-08 17:28:54
【问题描述】:

我是 Magento 的新手。我正在尝试为 Magento 管理面板开发一个自定义模块。我的代码如下

位置:app/etc/modules

Digitab_Brandlogo.xml

<?xml version="1.0" encoding="utf-8"?>
<config>
    <modules>
        <Digitab_Brandlogo>
            <active>true</active>
            <codePool>local</codePool>
        </Digitab_Brandlogo>
    </modules>
</config>

位置:app/code/local/Digitab/Brandlogo/Block/Adminhtml

logo.php

<?php
    class Digitab_Brandlogo_Block_Adminhtml_slider extends Mage_Adminhtml_Block_Widget_Grid_Container
    {
        public function __construct()
        {
                $this->_controller = 'adminhtml_brandlogo';
                $this->_blockGroup = 'brandlogo';
                $this->_headerText = Mage::helper('brandlogo')->__('Brand Logo Manager');
            $this->_addButtonLabel = Mage::helper('brandlogo')->__('Add Brand');
                parent::__construct();
        }
    }

位置:app/code/local/Digitab/Brandlogo/controllers/Adminhtml

IndexController.php

<?php

class Digitab_Brandlogo_Adminhtml_BrandlogoController extends Mage_Adminhtml_Controller_Action 
{
    public function indexAction()
    {                
           $this->loadLayout();
           $this->renderLayout();
    }
}

位置:app/code/local/Digitab/Brandlogo/等

config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <digitab_brandlogo>
            <version>1.0.0</version>
        </digitab_brandlogo>
    </modules>
    <global>
        <models />
        <blocks />
        <resources />
        <extraconfig />
        <helpers>
            <digitab_brandlogo>
                <class>Digitab_Brandlogo_Helper</class>
            </digitab_brandlogo>
        </helpers>
    </global>
    <admin>
        <routers>
            <digitab_brandlogo>
                <use>admin</use>
                <args>
                    <module>Digitab_brandlogo</module>
                    <frontName>brandlogo</frontName>
                </args>
            </digitab_brandlogo>
        </routers>
    </admin>
    <adminhtml>
        <layout>
        <updates>
            <brandlogo>
                <file>brandlogo.xml</file>
            </brandlogo>
        </updates>
    </layout>
    </adminhtml>
</config>

位置:app/code/local/Digitab/Brandlogo/等

adminhtml.xml

<?xml version="1.0"?>
<config>
    <menu>
        <digitab translate="title" module="digitab_brandlogo">
            <title>Digitab</title>
            <sort_order>110</sort_order>
            <children>
                <brandlogo>
                    <title>Brand Logo</title>
                    <sort_order>1</sort_order>
                    <action>brandlogo/adminhtml_brandlogo</action>
                </brandlogo>
            </children>
        </digitab>
    </menu>
</config>

位置:app/code/local/Digitab/Brandlogo/Helper

Data.php

<?php
class Digitab_Brandlogo_Helper_Data extends Mage_Core_Helper_Abstract
{
}

位置:app/design/adminhtml/default/default/layout

brandlogo.xml

<?xml version="1.0"?> 
<layout version="0.1.0">
    <brandlogo_adminhtml_brandlogo_index>
        <reference name="content">
            <block type="brandlogo/adminhtml_brandlogo" name="brandlogo" template="test.phtml"/>
        </reference>
    </brandlogo_adminhtml_brandlogo_index> 
</layout>

位置:app/design/adminhtml/default/default/template

test.phtml

ABCD

我得到如下输出

如何显示模板?任何人都可以在这方面帮助我吗?谢谢





更新

我修改了我的文件如下,但它不起作用。

位置:app/etc/modules

Digitab_Brandlogo.xml

<?xml version="1.0" encoding="utf-8"?>
<config>
    <modules>
        <Digitab_Brandlogo>
            <active>true</active>
            <codePool>local</codePool>
        </Digitab_Brandlogo>
    </modules>
</config>

位置:app/code/local/Digitab/Brandlogo/Block/Adminhtml

Brandlogo.php

<?php
    class Digitab_Brandlogo_Block_Adminhtml_Brandlogo extends Mage_Adminhtml_Block_Widget_Grid_Container
    {
        public function __construct()
        {
                $this->_controller = 'adminhtml_brandlogo';
                $this->_blockGroup = 'brandlogo';
                $this->_headerText = Mage::helper('brandlogo')->__('Brand Logo Manager');
            $this->_addButtonLabel = Mage::helper('brandlogo')->__('Add Brand');
                parent::__construct();
        }
    }

位置:app/code/local/Digitab/Brandlogo/controllers/Adminhtml

品牌标志控制器

<?php

class Digitab_Brandlogo_Adminhtml_BrandlogoController extends Mage_Adminhtml_Controller_Action 
{
    public function indexAction()
    {                
           $this->loadLayout();
           $this->renderLayout();
    }
}

位置:app/code/local/Digitab/Brandlogo/等

config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <digitab_brandlogo>
            <version>1.0.0</version>
        </digitab_brandlogo>
    </modules>
    <global>
        <models />
        <blocks>
            <brandlogo>
                <class>Digitab_Brandlogo_Block</class>
            </brandlogo>
        </blocks>
        <resources />
        <extraconfig />
        <helpers>
            <digitab_brandlogo>
                <class>Digitab_Brandlogo_Helper</class>
            </digitab_brandlogo>
        </helpers>
    </global>
    <admin>
        <routers>
            <digitab_brandlogo>
                <use>admin</use>
                <args>
                    <module>Digitab_brandlogo</module>
                    <frontName>brandlogo</frontName>
                </args>
            </digitab_brandlogo>
        </routers>
    </admin>
    <adminhtml>
    <layout>
        <updates>
            <brandlogo>
                <file>brandlogo.xml</file>
            </brandlogo>
        </updates>
    </layout>
    </adminhtml>
</config>

位置:app/code/local/Digitab/Brandlogo/等

adminhtml.xml

<?xml version="1.0"?>
<config>
    <menu>
        <digitab translate="title" module="digitab_brandlogo">
            <title>Digitab</title>
            <sort_order>110</sort_order>
            <children>
                <brandlogo>
                    <title>Brand Logo</title>
                    <sort_order>1</sort_order>
                    <action>brandlogo/adminhtml_brandlogo</action>
                </brandlogo>
            </children>
        </digitab>
    </menu>
</config>

位置:app/code/local/Digitab/Brandlogo/Helper

Data.php

<?php
class Digitab_Brandlogo_Helper_Data extends Mage_Core_Helper_Abstract
{
}

位置:app/design/adminhtml/default/default/layout

brandlogo.xml

<?xml version="1.0"?> 
<layout version="0.1.0">
    <brandlogo_adminhtml_brandlogo_index>
        <reference name="content">
            <block type="brandlogo/adminhtml_brandlogo" name="brandlogo" template="test.phtml"/>
        </reference>
    </brandlogo_adminhtml_brandlogo_index> 
</layout>

位置:app/design/adminhtml/default/default/template

test.phtml

ABCD

但它不起作用。现在我可以在管理面板中看到一个空白页。谢谢

【问题讨论】:

    标签: php xml magento admin


    【解决方案1】:

    这里有不少地方出错了,我将逐步检查每个文件。

    Digitab_Brandlogo.xml

    这里一切顺利。

    logo.php

    这里唯一的错误是您的文件名与您的类名不同(徽标!=滑块)。只需将文件名更改为类名,反之亦然,您应该对这个文件很好。另外,将你的类和文件名的第一个字母大写。

    IndexController.php

    这里与 logo.php 相同,您对文件的命名与类不同,但是您希望将文件名更改为类名,因为您已在许多其他文件中使用它.文件名应该是 BrandlogoController.php

    config.xml

    因为你定义了一个块,你想在全局下定义你的块。

    替换

    <blocks />
    

    用这个:

    <blocks>
        <digitab_brandlogo>
            <class>Digitab_Brandlogo_Block</class>
        </digitab_brandlogo>
    </blocks>
    

    然后,在您的路由器下,您要确保 Brandlogo 大写如下:

    <digitab_brandlogo>
        <use>admin</use>
        <args>
            <module>Digitab_Brandlogo</module>
            <frontName>brandlogo</frontName>
        </args>
    </digitab_brandlogo>
    

    adminhtml.xml

    您的操作是您想要在控制器中执行的操作。这个(在管理控制器上)的设置是 adminhtml/module_controller_action (如果你关闭操作,它默认为索引),所以在这种情况下你希望它是这样的:

    <action>adminhtml/brandlogo_brandlogo</action>
    

    Data.php

    这里一切顺利。

    brandlogo.xml

    您的标签需要具有 adminhtml_module_controller_action 格式,所以让我们将其更改为以下内容:

    <adminhtml_brandlogo_brandlogo_index>
    

    然后在这一行:

    <block type="brandlogo/adminhtml_brandlogo" name="brandlogo" template="test.phtml"/>
    

    您正在尝试使用虚构的块类型。还记得之前我们将块命名为 Logo 或 Slider 吗?这在这里发挥作用,因为这应该是您用于“类型”的文件路径。所以它应该看起来像以下之一:

    如果您选择了滑块

    <block type="brandlogo/adminhtml_slider" name="brandlogo" template="test.phtml"/>
    

    如果您选择了徽标

    <block type="brandlogo/adminhtml_logo" name="brandlogo" template="test.phtml"/>
    

    test.phtml

    这里一切顺利。

    希望这会有所帮助!

    【讨论】:

    • 感谢您的回复。现在我得到一个白页而不是 404 错误。你能帮我调试一下吗??
    • adminhtml/brandlogo_brandlogo 这会造成问题。可能这是错误的。
    • 尝试在 indexAction 的顶部添加以下内容:Mage::log("Brandlogo index");并查看当您导航到白屏时它是否出现在日志中。另外,你能告诉我它在哪个 URL 上进行白屏处理吗?
    • 我在日志下面。 2016-02-01T06:54:55+00:00 调试 (7): Brandllogo 索引
    • 我相信这也是问题所在。您也可以尝试在 Block 的 __construct 方法中添加类似的日志语句来检查它是否正在加载。
    【解决方案2】:

    这是一个完整的答案:

    位置:app/etc/modules

    Digitab_Brandlogo.xml

    看起来不错。

    位置:app/code/local/Digitab/Brandlogo/Block/Adminhtml

    此文件夹需要包含以下文件和文件夹:

    app/code/local/Digitab/Brandlogo/Block/Adminhtml/Brandlogo/Grid.php

    <?php 
    class Digitab_Brandlogo_Block_Adminhtml_Brandlogo_Grid extends Mage_Adminhtml_Block_Widget_Grid
    {
        public function __construct()
        {
            parent::__construct();
            $this->setId('digitab_brandlogo_grid');
            $this->setDefaultSort('increment_id');
            $this->setDefaultDir('DESC');
            $this->setSaveParametersInSession(true);
            $this->setUseAjax(true);
        }
    
        public function getGridUrl()
        {
            return $this->getUrl('*/*/grid', array('_current'=>true));
        }
    }
    

    app/code/local/Digitab/Brandlogo/Block/Adminhtml/Brandlogo.php

    <?php
    class Digitab_Brandlogo_Block_Adminhtml_Brandlogo extends Mage_Adminhtml_Block_Widget_Grid_Container
    {
        public function __construct()
        {
            $this->_blockGroup = 'digitab_brandlogo';
            $this->_controller = 'adminhtml_brandlogo';
            $this->_headerText = Mage::helper('digitab_brandlogo')->__('Brand Logo Manager');
    
            parent::__construct();
            $this->_addButtonLabel = Mage::helper('digitab_brandlogo')->__('Add Brand');
        }
    }
    

    位置:app/code/local/Digitab/Brandlogo/Helper/Data.php

    <?php
    class Digitab_Brandlogo_Helper_Data extends Mage_Core_Helper_Abstract {}
    

    位置:app/code/local/Digitab/Brandlogo/controllers/Adminhtml/BrandlogoController.php

    <?php
    class Digitab_Brandlogo_Adminhtml_BrandlogoController extends Mage_Adminhtml_Controller_Action 
    {
        public function indexAction()
        {                
            $this->loadLayout();
            $this->renderLayout();
        }
    }
    

    主要问题在于您编写 XML 文件的方式。

    位置:app/code/local/Digitab/Brandlogo/etc

    1. app/code/local/Digitab/Brandlogo/etc/config.xml 下面是config.xml 的代码。

    <?xml version="1.0"?>
    <config>
        <modules>
            <Digitab_Brandlogo>
                <version>1.0.0</version>
            </Digitab_Brandlogo>
        </modules>
        <global>
            <models />
            <blocks>
                <digitab_brandlogo>
                    <class>Digitab_Brandlogo_Block</class>
                </digitab_brandlogo>
            </blocks>
            <resources />
            <extraconfig />
            <helpers>
                <digitab_brandlogo>
                    <class>Digitab_Brandlogo_Helper</class>
                </digitab_brandlogo>
            </helpers>
        </global>
        <admin>
            <routers>
                <adminhtml>
                    <args>
                        <modules>                        
                            <digitab_brandlogo before="Mage_Adminhtml">Digitab_Brandlogo_Adminhtml</digitab_brandlogo>
                        </modules>
                    </args>
                </adminhtml>
            </routers>
        </admin>
        <adminhtml>
            <layout>
                <updates>
                   <brandlogo>
                        <file>brandlogo.xml</file>
                   </brandlogo>
                </updates>
            </layout>
        </adminhtml>
    </config>
    

    2. app/code/local/Digitab/Brandlogo/etc/adminhtml.xml 下面是config.xml 的代码。

    <?xml version="1.0"?>
    <config>
        <menu>
            <digitab translate="title" module="digitab_brandlogo">
                <title>Digitab</title>
                <sort_order>110</sort_order>
                <children>
                    <brandlogo>
                        <title>Brand Logo</title>
                        <sort_order>1</sort_order>
                        <action>adminhtml/brandlogo/index</action>
                    </brandlogo>
                </children>
            </digitab>
        </menu>
    </config>
    

    位置:app/design/frontend/adminhtml/default/default/layout/brandlogo.xml

    <?xml version="1.0"?> 
    <layout version="0.1.0">
        <adminhtml_brandlogo_index>
            <reference name="content">
                <block type="digitab_brandlogo/adminhtml_brandlogo" name="brandlogo" template="test.phtml"/>
            </reference>
        </adminhtml_brandlogo_index> 
    </layout>
    

    位置:app/design/frontend/adminhtml/default/default/template/test.phtml

    看起来不错。

    我已经在我的本地机器上检查了这段代码。在此处找到屏幕截图:

    让我知道这是否适合你。

    注意:请遵循我的回答中提到的确切命名约定和文件夹名称以及代码

    快乐编码...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-06
      • 1970-01-01
      • 2015-10-03
      • 2012-07-14
      • 1970-01-01
      相关资源
      最近更新 更多