Zend导航查找页面和渲染菜单及其子页面

时间:2013-04-16 21:07:28

标签: zend-framework navigation acl findby

我正在为具有4种不同访问级别的网站整合Zend Navigation:Guest,Member1,Member2和Admin。

我的导航XML看起来像这样:

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <nav>
        <default>
            <label>Home</label>
            <controller>index</controller>
            <action>index</action>
            <resource>index</resource>
            <privilege>index</privilege>

            <pages>
                <home>
                    ...
                </home>
                <signin>
                    ...
                </signin>
                <signup>
                    ...
                </signup>
            </pages>
        </default>

        <member1>
            <label>Member1 Main</label>
            <controller>member1</controller>
            <action>index</action>
            <resource>member1</resource>
            <privilege>index</privilege>

            <pages>
                <dashboard>
                    ...
                </dashboard>
                <settings>
                    <label>Settings</label>
                    <controller>auth</controller>
                    <action>editpassword</action>
                    <resource>auth</resource>
                    <privilege>editpassword</privilege>
                    <class>settings</class>
                    <title>User settings</title>

                    <pages>
                        <account>
                            ...
                        </account>
                        <logout>
                            ...
                        </logout>
                    </pages>
                </settings>
            </pages>
        </member1>

        <member2>
            <label>Member2 Main</label>
            <controller>member2</controller>
            <action>index</action>
            <resource>member2</resource>
            <privilege>index</privilege>

            <pages>
                <dashboard>
                    ...
                </dashboard>
                <profile>
                    ...
                </profile>
                <settings>
                    <label>Settings</label>
                    <controller>auth</controller>
                    <action>editpassword</action>
                    <resource>auth</resource>
                    <privilege>editpassword</privilege>
                    <class>settings</class>

                    <pages>
                        <account>
                            ...
                        </account>
                        <logout>
                            ...
                        </logout>
                    </pages>
                </settings>
            </pages>
        </member2>

        <admin>
            <label>Dashboard</label>
            <controller>admin</controller>
            <action>index</action>
            <resource>admin</resource>
            <privilege>index</privilege>

            <pages>
                <dashboard>
                    ...
                </dashboard>
                <logout>
                    ...
                </logout>
            </pages>
        </admin>

    </nav>
</config>

由于我使用子菜单并希望顶级菜单具有一致性,因此我想使用Zend的 findBy 功能来查找当前用户的状态并显示该菜单。这样做是这样的:

if ( $this->user ) {
    $submenu = $this->navigation()->findOneByLabel('Member1 Main');
    $options = array(
        'ulClass'               => 'navigation',
        'renderParents'         => true,
        'minDepth'              => null,
        'maxDepth'              => null
    );
    echo $this->navigation()->menu()->renderMenu($submenu, $options);
} else {
    echo $this->navigation()->menu()->setUlClass('navigation')->setOnlyActiveBranch(true)->setMinDepth(1)->setMaxDepth(1);
}

导航的我的Bootstrap位非常通用,看起来像这样:

function _initNavigation()
{
    $this->bootstrap('layout');
    $layout = $this->getResource('layout');
    $view = $layout->getView();

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

    $front     = Zend_Controller_Front::getInstance();
    $myPlagin  = $front->getPlugin('My_Controller_Plugin_Acl');

    $view->navigation($navigation)->setAcl($myPlagin->getMyAcl())
                                 ->setRole($myPlagin->getMyUserRole());
}

现在,我可以显示“Member1 Main”页面,但它只显示一个页面,但我需要呈现的是该页面的整个子菜单。似乎 findOneByLabel 仅查找该特定页面而不查找其后代。是否有办法拉动整个子菜单?

感谢。

1 个答案:

答案 0 :(得分:0)

原来我必须使用 findAllByLabel 来获得预期的结果。谢谢@RockyFord