zend 框架中的 Zend_Nav 问题导致菜单显示

发布于 2024-08-27 08:09:19 字数 2096 浏览 10 评论 0原文

我有以下问题,使用 zend 框架,特别是 zend_nav 创建一个可重用的菜单,通过layout/layout.phtml 页面传入。这些是各自文件中的代码片段。

首先在 application/configs/navigation.xml 中,

<configdata>
    <nav>      
         <label>Home</label>
         <controller>index</controller>
         <action>index</action>
         <pages>
             <add>
                 <label>Add</label>
                 <controller>post</controller>
                 <action>add</action>
             </add>
             <login>
                 <label>Admin</label>
                 <controller>login</controller>
                 <action>login</action>
             </login>
         </pages>     
     </nav>
 </configdata>

然后将其传递到 Bootstrap.php 文件中的一个对象(仅显示该特定方法)

protected function __initNavigation(){
    $this->bootstrap('layout');
    $layout = $this->getResource('layout');
    $view = $layout->getView();
    $config = new Zend_Config_Xml(APPLICATION .'/config/navigation.xml', 'nav');
    $container = new Zend_Navigation($config);
    $view->navigation($container);
}

,最后在视图 layout.phtml 中,该对象应返回菜单 菜单

 <!-- application/layouts/scripts/layout.phtml -->
<?php echo $this->doctype() ?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>        
    <title>Zend Blog !</title>
    <?php echo $this->headLink()->appendStylesheet('/css/global.css')?>
</head>
<body>
    <div id="header">
        <div id="header-logo">
            <b>Blog Me !</b>
        </div>
    </div>
    <div id="menu">            
        <?php echo $this->navigation()->menu() ?>
    </div>
    <div id="content">
    <?php echo $this->layout()->content ?>
    </div>
</body>
</html>

确实然而,当我在浏览器中启动我的应用程序时,它没有出现,任何关于可能出错的想法,都会虚心接受。

I have the following problem, using zend framework, specifically zend_nav to create a reusable menu to be passed in via the layout/layout.phtml page. These are the code fragments in their respective files.

first of in application/configs/navigation.xml,

<configdata>
    <nav>      
         <label>Home</label>
         <controller>index</controller>
         <action>index</action>
         <pages>
             <add>
                 <label>Add</label>
                 <controller>post</controller>
                 <action>add</action>
             </add>
             <login>
                 <label>Admin</label>
                 <controller>login</controller>
                 <action>login</action>
             </login>
         </pages>     
     </nav>
 </configdata>

this is then passed into an object in the Bootstrap.php file(only showing that specific method)

protected function __initNavigation(){
    $this->bootstrap('layout');
    $layout = $this->getResource('layout');
    $view = $layout->getView();
    $config = new Zend_Config_Xml(APPLICATION .'/config/navigation.xml', 'nav');
    $container = new Zend_Navigation($config);
    $view->navigation($container);
}

and then finally in the view layout.phtml, the object should return the menu

 <!-- application/layouts/scripts/layout.phtml -->
<?php echo $this->doctype() ?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>        
    <title>Zend Blog !</title>
    <?php echo $this->headLink()->appendStylesheet('/css/global.css')?>
</head>
<body>
    <div id="header">
        <div id="header-logo">
            <b>Blog Me !</b>
        </div>
    </div>
    <div id="menu">            
        <?php echo $this->navigation()->menu() ?>
    </div>
    <div id="content">
    <?php echo $this->layout()->content ?>
    </div>
</body>
</html>

The menu does however not show up when i start up my app in the browser, any ideas as to might have gone wrong, is humbly received.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

请止步禁区 2024-09-03 08:09:19

如果您没有故意使用两个 _ 下划线命名函数 __initNavigation,那么您可能希望代码自动运行。为了让它自动运行,您需要使用一个下划线。

另一个可能的问题是 _initNavigation_initView 之前运行,因为 Zend 按字母顺序浏览这些资源。但是您不需要在此代码中访问 $view 。您可以使用 Zend_Registry 来存储导航容器:

protected function _initNavigation() {
    $config = new Zend_Config_Xml(APPLICATION .'/config/navigation.xml', 'nav');
    $container = new Zend_Navigation($config);
    Zend_Registry::set('Zend_Navigation', $container);
}

当未指定容器时,任何导航助手都将默认使用 Zend_Navigation 注册表项。

If you did not name the function __initNavigation with two _ underscores on purpose then you probably expected the code to run automatically. For it to run automatically you need to use a single underscore.

Another possible problem is that _initNavigation runs before _initView as Zend goes through these resources in alphabetical order. But then you need not access $view in this code. You can use the Zend_Registry to store the navigation container:

protected function _initNavigation() {
    $config = new Zend_Config_Xml(APPLICATION .'/config/navigation.xml', 'nav');
    $container = new Zend_Navigation($config);
    Zend_Registry::set('Zend_Navigation', $container);
}

The Zend_Navigation registry entry will be used by default by any navigation helper when no container is specified.

星軌x 2024-09-03 08:09:19

好吧,我印象深刻,答案非常快,问题有几个方面,首先你们俩都正确地使用了单个下划线符号,非常感谢你们俩!
事实证明,我确实拼写错误,

$config = new Zend_Config_Xml(APPLICATION .'/config/navigation.xml', 'nav');

应该是

$config = new Zend_Config_Xml(APPLICATION_PATH .'/configs/navigation.xml', 'nav');

我的错。最后,在 navigation.xml 文件中,在 - 节点内,每个“页面”节点周围都应该有节点,例如主页。应该是

   <configdata>
     <nav> 
      <home>     
       <label>Home</label>
        <controller>index</controller>
        <action>index</action>
      </home>

这样吧!

再次,非常感谢您在正确的方向上提供的提示和技巧。

辛卡勒约翰逊

Well i`m impressed, really quick answers, the problem had several aspects, first of you where both right about using a single underscore sign, thanks a lot the both of you !
And as i t turned out, i did misspell,

$config = new Zend_Config_Xml(APPLICATION .'/config/navigation.xml', 'nav');

should be,

$config = new Zend_Config_Xml(APPLICATION_PATH .'/configs/navigation.xml', 'nav');

my fault. And finally a miss in the navigation.xml file, inside the - node, there should be nodes surrounding each of the "page" nodes, that is for instance for the home. It should have

   <configdata>
     <nav> 
      <home>     
       <label>Home</label>
        <controller>index</controller>
        <action>index</action>
      </home>

That was it !

Again, thanks a lot for your hints and tips i the right direction.

Sinc Kalle Johansson

筑梦 2024-09-03 08:09:19

我认为你的代码是正确的,只是你的受保护函数 __initNavigation()
应在 _initNavigation() 中仅使用一个 _

然后将 __initNavigation() 更改为 _initNavigation()

I think your code is correct, just your protected function __initNavigation()
should use just one _ in your _initNavigation()

Then change __initNavigation() to _initNavigation()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文