Php 如何在zend_导航的两个不同模块中使用两个不同的菜单?
我申请了两个模块。每个模块都有自己的菜单。我使用Zend_导航来呈现菜单。菜单正确呈现,子菜单也正确呈现,但每个页面上仅呈现一个菜单。我尝试了一些修复在这个论坛上发现,但没有改变 以下是我的结构:Php 如何在zend_导航的两个不同模块中使用两个不同的菜单?,php,zend-framework,menu,module,Php,Zend Framework,Menu,Module,我申请了两个模块。每个模块都有自己的菜单。我使用Zend_导航来呈现菜单。菜单正确呈现,子菜单也正确呈现,但每个页面上仅呈现一个菜单。我尝试了一些修复在这个论坛上发现,但没有改变 以下是我的结构: application/ modules/ default/ configs/ navigationSite.xml controllers/ models/
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()
);
}
}
set('Zend_NavigationSite',$containerSite);
//添加操作助手
Zend\u控制器\u操作\u HelperBroker::addHelper(
新的Bravo_控制器(动作)助手(导航站点)
);
}
}
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()
);
}
}
set('Zend_NavigationApp',$containerCards);
//添加操作助手
Zend\u控制器\u操作\u HelperBroker::addHelper(
新的Bravo_控制器(动作)助手(导航应用)
);
}
}
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>
卡片1
/卡片/菜单1
卡片2
/卡片/菜单2
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>
场地1
/默认/菜单1
站点2
/默认/菜单2
在我看来,默认情况下:
<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_注册表”。现在,我正在调用正确的索引,一切正常!:-)
我也更新了这篇文章的代码。
感谢各位提供的备选解决方案 我的项目中通常有多个菜单,但我只使用一个带开关的引导 根据“帐户角色”,我得到了不同的菜单和布局,但方式不同:
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->\u acl和一个$this->\u auth,它们是Zend_acl和Zend_auth的实例,用于权限相关的内容:)
我的导航(示例):
登录
保护
登录
违约
登录
我可以展示如何使用一个引导在不同的模块中创建不同的菜单。我在项目中使用控制器插件:
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);
}
}
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);
}
}