Zend Framework:导航 XML 和重复的页面元素
在 XML 中,我通常期望以下内容完全有效,并且可以使用 PHP 的 DomDocument 之类的有意义的方式进行导航:
<?xml version="1.0" encoding="UTF-8"?>
<configdata>
<page>
<name>Home</name>
</page>
<page>
<name>Log in</name>
</page>
</configdata>
使用 Zend_Navigation
时情况并非如此。每个
元素都需要有一个唯一的名称,因此您需要这样做:
<?xml version="1.0" encoding="UTF-8"?>
<configdata>
<page_home>
<name>Home</name>
</page_home>
<page_log_in>
<name>Log in</name>
</page_log_in>
</configdata>
这可行,但非常烦人。我宁愿拥有多个页面元素,这些元素可以具有相同的名称,并且在创建许多导航页面时可以轻松复制和粘贴。
为什么每个人都需要一个唯一的名字?
有没有办法不必拥有唯一的名称?
@Charles
是的,以下代码用于读取导航 XML
$config = new Zend_Config_Xml(APPLICATION_PATH . '/configs/navigation.xml');
$container = new Zend_Navigation($config);
Zend_Registry::set("navigation", $container);
@Gordon
好问题...我曾经使用过这种方法,但想要另一种更简单的方法更新和阅读。数组表示法确实解决了我遇到的问题,但这并不是一种编写网站导航的简单方法,特别是当存在嵌套元素时。 XML 比 PHP 的数组更容易阅读和理解。
当然,这是我自己的观点,它是存储和解析导航数据的较慢的方式。
In XML I'd normal expect the following to be perfectly valid and navigable in a meaningful way using something like PHP's DomDocument:
<?xml version="1.0" encoding="UTF-8"?>
<configdata>
<page>
<name>Home</name>
</page>
<page>
<name>Log in</name>
</page>
</configdata>
This is not the case when using Zend_Navigation
. Each <page>
element needs to have a unique name, so you would need to do:
<?xml version="1.0" encoding="UTF-8"?>
<configdata>
<page_home>
<name>Home</name>
</page_home>
<page_log_in>
<name>Log in</name>
</page_log_in>
</configdata>
This works, but is very annoying. I'd much rather have multiple page elements which can have the same name and can be easily copy and pasted when creating many pages for navigation.
Why does each one need a unique name?
Is there a way of not having to have a unique name?
@Charles
Yes, the following code is used to read in the navigaion XML
$config = new Zend_Config_Xml(APPLICATION_PATH . '/configs/navigation.xml');
$container = new Zend_Navigation($config);
Zend_Registry::set("navigation", $container);
@Gordon
Good question...I used to use this method, but wanted another way that was easier to update and read. The array notation does solve the issue I have but it isn't an easy way of writing out the navigation for a site, especially when there are nested elements. XML is much easy to read and make sense of than PHP's arrays.
Granted this is my own opinion and it is a slower way of storing and parsing navigation data.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不能使用第一个 XML 结构,因为
Zend_Navigation
使用 Tag 定义来创建“Route”的一部分。如果您想使用另一种类型的 XML 结构,您可能必须使用您自己的解析过程来扩展Zend_Navigation
。另一种方法是创建一个类来解析和修改 XML 文档,然后再将其发送到
Zend_Navigation
。You can't use the first XML structure, because
Zend_Navigation
uses the Tag definition to create a part of the "Route". If you want use an another type of XML structure, you probably have to extendZend_Navigation
with your own parsing process.Another way would be to create a class to parse and modify the XML document before sending it to
Zend_Navigation
.