Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在Magento中指定块名?_Php_Magento_Zend Framework - Fatal编程技术网

Php 如何在Magento中指定块名?

Php 如何在Magento中指定块名?,php,magento,zend-framework,Php,Magento,Zend Framework,这是一个基本的问题,但这让我在过去的几天里失眠 我编写了一个带有控制器、phtml模板和块类的定制模块,如下所示: app/code/local/Huahan/HelloWorld/Block php(其中Huahan\u Helloworld\u Block\u Helloworld扩展了Mage\u Core\u Block\u模板) 应用程序/代码/本地/华瀚/HelloWorld/控制器 IndexController.php(其中定义了索引控制器的indexAction) app/de

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

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

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

在布局helloworld.xml中,我在块标记中指定模板文件

<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\u Helloworld\u Block\u Helloworld是我希望在呈现任何输出之前加载的块类?

是否有关于MagentoXML的详细文档?我总是对MagentoXML语法的复杂性和随意性感到沮丧

Magento的版本是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>

0.1.0
标准
华汉地狱世界
地狱世界
helloworld.xml
华瀚湖Helloworld湖区块
提前谢谢。我真的很感谢你在这里读到它。

我明白了

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

只需将helloworld.xml的block项中type=“core/template”的值更改为type=“Huahan\u helloworld\u block\u helloworld”

那就完了

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

块类型 块
类型
属性是配置a类块使用的项。不鼓励在
类型中使用完整的类名,尽管可能

Magento中的类别名系统的目的是允许您灵活地扩展/覆盖自己模块中的核心/模块功能

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

块配置 在模块声明中,必须指定块前缀,例如:

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

华瀚湖Helloworld湖区块
根据Magento惯例,
部分应为
huahan_helloworld
helloworld


由于这些配置和其他Magento约定,您的块类型应分别为
huahan_helloworld/helloworld
helloworld/helloworld

这不应该起作用。块类型不是块类名的普通字符串,它遵循
组/block\u名称
结构。我不知道您指定的组名是什么,因为我看不到您的
config.xml
文件。@ZeLoubs是正确的-根据您的config.xml文件,您的类型可能是
huahan\u helloworld/helloworld
或者
helloworld/helloworld
。粘贴到config.xml文件中,我们可以为您提供更明确的答案。谢谢。我已经在问题正文中发布了config.xml。它确实有效。@ZeLoubs AFAIK来自Magento internals我见过一些代码,这些代码首先检查字符串中是否有
/
,如果没有,它会尝试直接将字符串用作类。@ZeLoubs因此,事实上直接使用类是可行的,但由于多种原因,不建议这样做。