【问题标题】:How to specify the block name in Magento?如何在 Magento 中指定块名称?
【发布时间】:2014-11-24 01:03:39
【问题描述】:

这是一个基本问题,但过去几天让我失眠。

我编写了一个带有控制器、phtml 模板和块类的自定义模块,如下所示: app/code/local/Huahan/HelloWorld/Block Helloworld.php(其中 Huahan_Helloworld_Block_Helloworld 扩展了 Mage_Core_Block_Template) app/code/local/Huahan/HelloWorld/controllers IndexController.php(其中定义了索引控制器的 indexAction) app/design/frontend/base/default/template/huahan/helloworld helloworld.phtml(定义视图的地方) 应用程序/设计/前端/基础/默认/布局 helloworld.xml(文件名“helloworld.xml”在etc/config.xml中指定)

我知道 Magento 中的每个请求处理都是从控制器及其操作方法开始的。

在布局helloworld.xml中,我在block标签中指定了模板文件

<helloworld_index_nihao>
<block type="core/template" name="just_an_arbitary_name" output="toHtml" template="huahan/helloworld/nihao.phtml"/>
</helloworld_index_nihao>
</layout> 

因此控制器在处理请求时知道使用哪个模板。

问题是如何让 Magento 知道 Huahan_Helloworld_Block_Helloworld 是我希望它在渲染任何输出之前加载的块类?

如果有任何关于 Magento xml 的详细文档?我总是对 Magento xml 语法的复杂性和随意性感到沮丧

Magento的版本是ver.1.9.0.1

config.xml如下

<?xml version="1.0"?>
<config>
<modules>
<huahan_helloworld>
<version>
0.1.0
</version>
</huahan_helloworld>
</modules>


<frontend>
<routers>
<!-- the <helloworld> tagname appears to be arbitrary, but by
convention is should match the frontName tag below-->
        <helloworld>
                <use>standard</use>
                <args>
                        <module>Huahan_HelloWorld</module>
                        <frontName>helloworld</frontName>
                </args>
        </helloworld>
</routers>

<layout>
        <updates>
                <helloworld>
                        <file>helloworld.xml</file>
                </helloworld>
        </updates>
</layout>
</frontend>

<global>
        <blocks>
                <helloworld>
                        <class>Huahan_Helloworld_block</class>
                </helloworld>
        </blocks>
</global>

</config>

提前致谢。非常感谢您在此处阅读。

【问题讨论】:

    标签: php magento zend-framework


    【解决方案1】:

    块类型

    方块type属性是配置方块使用什么类的项目。尽管可能,但不鼓励在 type 中使用完整的类名。

    Magento 中类别名系统的目的是在您自己的模块中扩展/覆盖核心/模块功能时为您提供灵活性。

    如果每个人都使用类别名,那么您可以用自己的类替换/覆盖别名,并“注入”您的行为而不是原始行为。这是一种解耦代码的形式,以允许可扩展性。

    块配置

    在您的模块声明中,您已经必须指定一个块前缀,例如:

    <blocks>
        <huahan_helloworld>
            <class>Huahan_Helloworld_Block</class>
        </huahan_helloworld>
    </blocks>
    

    根据 Magento 约定,&lt;class&gt; 部分应为 huahan_helloworldhelloworld

    由于这些配置和其他 Magento 约定,您的块类型应该是 huahan_helloworld/helloworldhelloworld/helloworld(分别)。

    【讨论】:

      【解决方案2】:

      我想通了。

      布局xml文件是我应该工作的地方。

      只需将helloworld.xml的block项中type="core/template"的值改为type="Huahan_Helloworld_Block_Helloworld"

      然后就完成了!

      我讨厌 Magento,“核心/模板”从任何角度来看都不像一个类名!

      【讨论】:

      • 这应该行不通。块类型不是块类名的纯字符串,它遵循group/block_name 结构。我不知道您指定的组名是什么,因为我看不到您的 config.xml 文件。
      • @ZeLoubs 是正确的 - 根据您的 config.xml 文件,您的类型可能是 huahan_helloworld/helloworldhelloworld/helloworld。粘贴到您的 config.xml 文件中,我们可以给您更明确的答案。
      • 谢谢。我已经在问题正文中发布了 config.xml。它确实有效。
      • @ZeLoubs AFAIK from Magento internals 我看过代码,它首先检查字符串中是否有/,如果没有,它会尝试直接将字符串用作类。
      • @ZeLoubs 所以实际上直接使用该类是可行的,但出于多种原因不建议这样做。
      猜你喜欢
      • 2014-11-05
      • 2017-07-13
      • 2013-03-31
      • 2012-04-12
      • 2017-04-02
      • 2017-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多