Zend Navigation - 渲染子菜单

时间:2012-04-24 12:12:01

标签: zend-framework zend-navigation

我将所有页面定义在XML文件中,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<configdata>
<nav>
    <dashboard>
        <label>Dashboard</label>
        <module>default</module>
        <controller>dashboard</controller>
        <action>index</action>
        <title>Die Schaltzentrale</title>
    </dashboard>
    <user>
        <label>User</label> 
        <module>default</module>
        <controller>user</controller>   
        <action>index</action>
        <title>Verwaltung der Benutzer</title>

        <userList>                  
            <module>default</module>
            <controller>user</controller>   
            <action>index</action>
            <label>Userliste anzeigen</label>
        </userList>
        <newUser>
            <label>User anlegen</label> 
            <module>default</module>
            <controller>user</controller>   
            <action>new</action>
        </newUser>
        <editUser>
            <label>User bearbeiten</label>  
            <module>default</module>
            <controller>user</controller>   
            <action>edit</action>
        </editUser>
    </user>
</nav>  
</configdata>

在我的bootstrap中,我设置了这样的导航:

    protected function _initNavigation()
    {
        $this->bootstrap('layout');
        $layout = $this->getResource('layout');
        $config = new Zend_Config_Xml(APPLICATION_PATH . '/configs/navigation.xml','nav');
        $view = $layout->getView();
        $navigation = new Zend_Navigation($config);
        $view->navigation($navigation); 
        Zend_Registry::set('Zend_Navigation',$navigation);
    }

此设置使我能够使用以下行渲染主菜单:

$partial = array('menu.phtml', 'default');
$this->navigation()->menu()->setPartial($partial);
echo $this->navigation()->menu()->render();

到目前为止一切顺利。我现在的问题是渲染一个特定的子菜单。假设我想渲染一个菜单,其中包含用户控制器的所有操作。我试着用它来渲染它:

$page = $this->navigation()->findOneBy('controller','user');
echo $this->navigation()->menu()->renderMenu($page);

但我没有输出。我还尝试通过设置minDepth或maxDepth选项获得输出,但没有成功。有没有人跟我说过,我怎么能把它带到工作中去?

1 个答案:

答案 0 :(得分:2)

你的方法非常接近。将findOneBy行更改为:

$page = $this->navigation()->findOneBy('label','User');

这将获取用户页面下的所有页面。

我认为不可能通过控制器找到一个页面。

[编辑]

我已在用户下添加“页面”部分修改了您的xml。这告诉Zend Navigation userList,newUser和editUser是user的子页面:

<?xml version="1.0" encoding="UTF-8"?>
<configdata>
<nav>
    <dashboard>
        <label>Dashboard</label>
        <module>default</module>
        <controller>dashboard</controller>
        <action>index</action>
        <title>Die Schaltzentrale</title>
    </dashboard>
    <user>
        <label>User</label> 
        <module>default</module>
        <controller>user</controller>   
        <action>index</action>
        <title>Verwaltung der Benutzer</title>
        <pages>
            <userList>
                <module>default</module>
                <controller>user</controller>   
                <action>index</action>
                <label>Userliste anzeigen</label>
            </userList>                
            <newUser>
                <label>User anlegen</label> 
                <module>default</module>
                <controller>user</controller>   
                <action>new</action>
            </newUser>
            <editUser>
                <label>User bearbeiten</label>  
                <module>default</module>
                <controller>user</controller>   
                <action>edit</action>
            </editUser>
        </pages>
    </user>
</nav>  
</configdata>