【问题标题】:Magento Block not LoadingMagento 块未加载
【发布时间】:2019-06-21 21:08:01
【问题描述】:

我正在 Magento 中构建我的第一个模块,并且有几个与该过程相关的问题。

在我尝试一个模块之前,我只有一个模板,并使用此代码将其加载到 app\design\frontend\rwd\default\layout\local.xml

<?xml version="1.0"?>
<layout version="0.1.0">
    <default>
        <reference name="after_body_start">
            <block type="checkout/cart_sidebar" template="mgw/mwCartRebuild.phtml"/>
        </reference>
    </default>
</layout>

生活很美好,一切正常,直到我决定需要扩展 Mage_Checkout_Block_Cart_Sidebar 类。

所以我创建了一个模块来做到这一点。这是我的代码。

屏蔽 app\code\local\mgw\Cart\Block\ModalCart.php

<?php 
class mgw_Cart_Block_Modal_Cart extends Mage_Checkout_Block_Cart_Sidebar{
    public function __construct(){
        perent::__construct();
        $this->setTemplate('mgw/mwCartRebuild.phtml');
    }
}

config.xml app\code\local\mgw\Cart\etc\config.xml

<config>
    <global>
        <modules>
            <mgw_Cart>
                <version>0.0.0</version>
            </mgw_Cart>
        </modules>
        <blocks>
            <mgw_Cart>
                <class>mgw_Cart_Block_Modal_Cart</class>
            </mgw_Cart>
        </blocks>
        <helpers>
            <cart>
                <class>mgw_Cart_Helper</class>
            </cart>
        </helpers>
    </global>
</config>

新的 local.xml app\design\frontend\rwd\default\layout\local.xml

<layout version="0.1.0">
    <default>
        <reference name="after_body_start">
            <block type="cart/modal_cart"/>
        </reference>
    </default>
</layout>

模块 xml app\etc\modules\mgw_Cart.xml

<config>
    <modules>
        <mgw_Cart>
            <active>true</active>
            <codePool>local</codePool>
            <depends />
        </mgw_Cart>
    </modules>
</config>

现在我的模板无法加载。我已经检查了管理员以查看我的模块是否正在加载并已列出。那么为什么我的模板无法加载呢?

我的问题是:

  • 我什至可以像我尝试的那样扩展核心块吗?
  • 如何让我的块/模板加载?

【问题讨论】:

    标签: php xml magento


    【解决方案1】:

    我是 Magento 的新手,我可以帮助您解决问题。 首先,我建议为您的自定义模块和文件使用清晰简单的命名法,“ModalCart.php”可以重命名为“Modalcart.php”,避免任何引用问题。

    如果您想扩展/覆盖一个 Mage 类,您需要在模块的 config.xml 中指定它,如下所示:

    <blocks>
        <checkout>
            <rewrite>
                <cart_sidebar>PkgName_ModuleName_Block_YourClassThatOverrides</cart_sidebar>
            </rewrite>
        </checkout>
    </blocks>
    

    在上面的代码中,你声明你将用你的新类重写 checkout/cart_sidebar 块。

    所以这是 config.xml,现在您要创建覆盖的类。在模块的 Block 目录中创建扩展/重写核心类的类文件 .php:

    <?php
      class PkgName_ModuleName_Block_YourClassThatOverrides extends Mage_Checkout_Block_Cart_Sidebar {
          // check for the methods to rewrite or create new methods
      }
    

    关于如何设置特定模板或布局的最后一个问题: 在开始编码之前,我个人的建议是从不同的来源学习相同的东西 3 到 4 次,然后再编码 3 到 4 次,直到你能够理解并记住所有内容。 所以对于布局/模板部分,我建议阅读这个 Alan Storm 线程: https://alanstorm.com/layouts_blocks_and_templates/

    【讨论】:

    • 感谢 Marco,这是很好的信息。学习 magento 1 有很多不完整的资源。很难将所有内容拼凑在一起。我会尽快尝试所有这些。
    • 这很难,因为没有免费的直线指南可以遵循,就像他们做的一样 explode(" " ,MagentoGuide1.x) 。只要继续锻炼,你就会爬上那个知识边缘。我很高兴解决了您的问题。
    • 仍然无法在&lt;reference name="after_body_start"&gt; 上加载自定义块
    • NVM。我想到了。我的块类文件命名不正确。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-11
    • 1970-01-01
    • 2016-01-21
    • 1970-01-01
    • 2012-07-26
    • 1970-01-01
    相关资源
    最近更新 更多