【问题标题】:How to reference the right layout file in Magento如何在 Magento 中引用正确的布局文件
【发布时间】:2014-02-21 20:46:59
【问题描述】:

我正在尝试创建一个扩展程序以在管理区域中显示自定义网格。我的控制器正在工作,我可以从 indexAction 打印一个“Hello World”,但是我无法让它使用我定义的布局。它显示一个空白的内容区域并且没有错误。如果我使布局文件 XML 结构无效,则没有错误。这告诉我 indexAction 中的布局规范被忽略了。我尝试了很多方法来引用它,但不起作用。所有必要的文件都已到位。看来我的问题出在布局的路径上(用代码中的注释表示://Problem HERE!)。如果我使用 'adminhtml/sales_invoice' 它可以工作,所以这是我的路径的问题。有人知道我如何准确地引用下面给出的结构的布局吗?

mycompany
-myextension
--Block
---adminhtml
----Container
-----Grid.php
----Container.php
--controllers
---adminhtml
----IndexController.php
--etc

这是我的索引控制器:

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

        //The problem is HERE!
    $this->_addContent($this->getLayout()->createBlock('dont know/what_goes_here'))
        ->renderLayout();
    }
}

这是我的config.xml(与布局相关的部分)

<adminhtml>
    <layout>
        <updates>
            <myextension>
                <file>mycompany/advancedsalesreport.xml</file>
            </myextension>
        </updates>
    </layout>
</adminhtml>

【问题讨论】:

    标签: magento layout module controller admin


    【解决方案1】:

    在你的控制器中使用

    <?php
    class Surpassweb_AdvancedSalesReport_Adminhtml_IndexController extends Mage_Adminhtml_Controller_Action {
        public function indexAction(){
            $this->loadLayout();
    
            //The problem is HERE!
        $this->_addContent($this->getLayout()->createBlock('AdvancedSalesReport/adminhtml_Container_Grid'))
            ->renderLayout();
        }
    }
    

    在你的 Grid.php 中结束

    <?php
    
    class class Surpassweb_AdvancedSalesReport_Block_adminhtml_Grid extends Mage_Catalog_Block_Product_Abstract implements Mage_Widget_Block_Interface
    {
        public function __construct()
        {
          parent::__construct();
          $this->setTemplate('AdvancedSalesReport/yourform.phtml');
        }
    }
    

    模型路径:

    -app
    --design
    ---adminhtml
    ----default
    -----default
    ------template
    -------AdvancedSalesReport
    --------yourform.phtml
    

    【讨论】:

    • 在这种情况下我还需要一个布局文件吗?
    • 我试过但没有用,但其他答案有效,所以我坚持下去。不过,感谢您的宝贵时间,我很感激。
    【解决方案2】:
    $this->getLayout()->createBlock('dont know/what_goes_here')
    

    应该是

    $this->getLayout()->createBlock('advancedsalesreport/adminhtml_container')
    

    advancedsalesreport 来自 config.xml 的这一部分

    <global>
            <blocks>
                <advancedsalesreport>
                    <class>Surpassweb_AdvancedSalesReport_Block</class>
                </advancedsalesreport>
            </blocks>
    </global>
    

    但是,与其在控制器中使用 createBlock,我更愿意在您的案例中定义布局文件中的块 (mycompany/advancedsalesreport.xml)

    <?xml version="1.0"?>
    <layout version="0.1.0">
        <yourroute_adminhtml_index_index>
            <reference name="content">
                <block type="advancedsalesreport/adminhtml_stock_purchaseOrder_new" name="new_purchase_order" />
            </reference>
        </yourroute_adminhtml_index_index>
    </layout>
    

    yourroute 与 config.xml 的 admin > routers 部分匹配的位置

    【讨论】:

    • 宾果游戏!这行得通!只是为了澄清......我使用了第一种方法。要使用第二个我没有在配置中定义 部分?有必要吗?在
    • 已按要求编辑,抱歉刚刚从我正在处理的模块中复制了它。
    • 无需道歉,您已经提供了很多帮助。谢谢。你会考虑看看这个问题:stackoverflow.com/questions/21396229/…
    猜你喜欢
    • 2011-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-16
    • 1970-01-01
    • 2012-12-12
    • 2012-11-25
    • 1970-01-01
    相关资源
    最近更新 更多