如何使用zend_navigation在两个不同的模块中使用两个不同的菜单?

时间:2011-12-23 08:54:47

标签: php zend-framework menu module

我用两个模块制作了一个应用程序。每个模块都有自己的菜单。我使用Zend_navigation来渲染菜单。菜单正确呈现,子菜单也可以,但每个页面上只有一个菜单呈现。我在这个论坛上尝试了一些修复,但没有任何改变。

这是我的结构:

application/
    modules/
        default/
            configs/
               navigationSite.xml
            controllers/
            models/
            views/
               bootstrap.php
        cards/
            configs/
               navigationCards.xml
            controllers/
            models/
            views/
               bootstrap.php

Boostrap模块默认值:

<?php

class Default_Bootstrap extends Zend_Application_Module_Bootstrap
{

    protected function _initNavigation()
    {

            // read navigation XML and initialize container
            $configSite = new Zend_Config_Xml(
                                    APPLICATION_PATH.
                                        '/modules/default/configs/navigationSite.xml', 
                                        'site');
            $containerSite = new Zend_Navigation($configSite);
            // register navigation container
            $registrySite = Zend_Registry::getInstance();
            $registrySite->set('Zend_NavigationSite', $containerSite);
            // add action helper
            Zend_Controller_Action_HelperBroker::addHelper(
                new Bravo_Controller_Action_Helper_NavigationSite()
            );
    }

}

Boostrap模块卡:

<?php

class Cards_Bootstrap extends Zend_Application_Module_Bootstrap
{

    protected function _initNavigation()
    {

            // read navigation XML and initialize container
            $configCards = new Zend_Config_Xml(
                                    APPLICATION_PATH.
                                        '/modules/default/configs/navigationCards.xml', 
                                        'site');
            $containerCards = new Zend_Navigation($configCards);
            // register navigation container
            $registryCards = Zend_Registry::getInstance();
            $registryCards->set('Zend_NavigationApp', $containerCards);
            // add action helper
            Zend_Controller_Action_HelperBroker::addHelper(
                new Bravo_Controller_Action_Helper_NavigationApp()
            );
    }

}

navigationCards.xml:

<?xml version="1.0" encoding="UTF-8"?>

<configData>
    <cards>
        <menu1>
            <label>Cards 1</label>
            <uri>/cards/menu1</uri>
        </menu1>
        <menu2>
            <label>Cards 2</label>
            <uri>/cards/menu2</uri>
        <menu2>
    </cards>
</configData>

navigationSite.xml:

<?xml version="1.0" encoding="UTF-8"?>

<configData>
    <site>
        <menu1>
            <label>Site 1</label>
            <uri>/default/menu1</uri>
        </menu1>
        <menu2>
            <label>Site 2</label>
            <uri>/default/menu2</uri>
        <menu2>
    </site>
</configData>

在我看来默认:

<div id="menu">
    <?php echo $this->navigation(Zend_Registry::get('Zend_NavigationSite'))->menu();?>
</div>

在我看来卡片:

<div id="menu">
    <?php echo $this->navigation(Zend_Registry::get('Zend_NavigationApp'))->menu()
    ->renderMenu(null, array(
        'minDepth' => null,
        'maxDepth' => 1,
        'ulClass' => 'navigation',
        'onlyActiveBranch' => false));
    ?>
</div>

有些东西阻止在自己的页面上渲染两个不同的菜单,但我无法想象。也许有人会看到明显的: - )

编辑:我修好了!我只是不知道什么是“Zend_Registry”。现在,我正在调用正确的索引,一切正确! :-) 我也更新了这篇文章的代码。 谢谢大家的替代解决方案!

2 个答案:

答案 0 :(得分:1)

为了它的价值,我可以展示如何使用一个引导程序在不同的模块中制作不同的菜单。我在项目中使用了一个控制器插件:

class Application_Plugin_Menu extends Zend_Controller_Plugin_Abstract
{
    public function preDispatch(Zend_Controller_Request_Abstract $request)
    {
        $module = strtolower($request->getModuleName());
        $controller = strtolower($request->getControllerName());

        $nav = new Zend_Navigation(new Zend_Config_Ini($config, $module));
        Zend_Registry::set('Zend_Navigation', $nav);   
    }
}

答案 1 :(得分:0)

我的项目中通常有多个菜单,但我只使用一个带开关的Bootstrap。

基于“帐户角色”,我得到了不同的菜单和不同的布局,但方式不同:

protected function _initNavigationAndLayout(){
    $this->bootstrap('layout');
    $layout = $this->getResource('layout');
    $view = $layout->getView();
    switch ($examplevar){
        default:
        case 1:
            $layout->setLayout('layout1');
            $config = new Zend_Config_Xml(APPLICATION_PATH . '/configs/navigation1.xml','nav');
            break;
        case 2:
            $layout->setLayout('layout2');
            $config = new Zend_Config_Xml(APPLICATION_PATH . '/configs/navigation2.xml','nav');
            break;
    }
    $navigation = new Zend_Navigation($config);
    $view->navigation($navigation)->setAcl($this->_acl)->setRole($this->_auth->getStorage()->read()->role    );
}

你可以看到我有一个$ this-&gt; _acl和一个$ this-&gt; _auth,它们是Zend_Acl和Zend_Auth的实例,用于许可相关的东西:)

我的导航(示例):

<?xml version="1.0" encoding="UTF-8"?>
    <configdata>
        <nav>
            <login>
                <label>Login</label>
                <controller>secure</controller>
                <action>login</action>
                <route>default</route>
                <resource>login</resource>
            </login>
        </nav>
    </configdata>