Zend框架中的Zend_Nav问题让菜单显示出来

问题描述 投票:1回答:3

我有以下问题,使用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>

但是,当我在浏览器中启动我的应用程序时,菜单确实没有显示,任何可能出错的想法都是谦虚地收到的。

zend-framework zend-navigation
3个回答
1
投票

如果你没有故意用两个__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注册表项。


1
投票

嗯,我印象深刻,答案很快,问题有几个方面,首先你们两个正确使用一个下划线标志,非常感谢你们两个人!事实证明,我做错了,

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

应该,

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

我的错。最后,在nav.xml文件中,在 - 节点内部,应该有每个“页面”节点周围的节点,例如用于家庭。应该有

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

就是这样!

再次,非常感谢你的提示和技巧,我正确的方向。

Sinc Kalle Johansson


1
投票

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

然后将__initNavigation()改为_initNavigation()

© www.soinside.com 2019 - 2024. All rights reserved.