我照我说的做了 :) 该功能非常简单且富有弹性。
导航.php
$siteMenu = array(
array(
'resource' => 'superadmin:index',
'privilege' => 'index',
'label' => 'Dashboard',
'module' => 'superadmin',
'controller' => 'index',
'action' => 'index',
'route' => 'superadmin',
'icon' => 'icon-home',
),
// ====================================== USERS
array(
'resource' => 'superadmin:users',
'privilege' => 'index',
'label' => 'Users',
'module' => 'superadmin',
'controller' => 'users',
'action' => 'index',
'route' => 'superadmin',
'position' => 'top|sidebar',
'accessable' => 'users:*|multid:*',
'pages' => array(
array(
'resource' => 'superadmin:users',
'privilege' => 'index',
'label' => 'Users list',
'module' => 'superadmin',
'controller' => 'users',
'action' => 'index',
'route' => 'superadmin',
'icon' => 'icon-list',
'position' => 'top|sidebar',
'accessable' => 'users:*|multid:*',
),
View Helper 将有助于扩展自定义导航属性:
<?php
/**
* Helper for building menu with Zend_Navigation
*
* @author Szymon Bluma <szbluma@gmail.com>
*/
class MrBlue_Helper_Navigation
{
/**
* Is the $page visible on sidebar?
*
* @param Zend_Navigation_Page $page
* @return boolean
*/
public static function isSidebar(Zend_Navigation_Page $page)
{
if( isset($page->position) && in_array('sidebar',explode('|',$page->position)) ) {
return true;
}
return false;
}
/**
* Is the #page visible on topmenu?
*
* @param Zend_Navigation_Page $page
* @return boolean
*/
public static function isTopmenu(Zend_Navigation_Page $page)
{
if( !isset($page->position) ) {
return true;
}
if( in_array('top',explode('|',$page->position)) ) {
return true;
}
return false;
}
/**
* Is the $page (mainly in sidebar) accessable in $position type menu?
*
* @param Zend_Navigation_Page $page
* @param string $position OPTIONAL menu type - top or sidebar
* @return boolean
*/
public static function isAccessable(Zend_Navigation_Page $page, $position='top')
{
if( $position=='top' || !isset($page->accessable) ) {
return true;
}
$controller = Zend_Controller_Front::getInstance()->getRequest()->getControllerName();
$action = Zend_Controller_Front::getInstance()->getRequest()->getActionName();
$elements = explode('|',$page->accessable);
if( in_array($controller.':*',$elements) || in_array($controller.':'.$action,$elements) ) {
return true;
}
return false;
}
}
以及 menu_left.phtml 视图文件:
<?php if($this->container): ?>
<ul class="nav nav-list">
<?php foreach ($this->container as $page): ?>
<?php if($this->navigation()->accept($page) && MrBlue_Helper_Navigation::isAccessable($page, 'sidebar')): ?>
<?php if($page->isVisible() && MrBlue_Helper_Navigation::isSidebar($page)): ?>
<?php if(count($page->pages) > 0): ?>
<li class="nav-header"><?php if(isset($page->icon)): ?><span class="<?= $page->icon; ?> icon"></span> <?php endif; ?><?= $page->label; ?></li>
<?php foreach ($page->pages as $subPage): ?>
<?php if($subPage->isVisible() && MrBlue_Helper_Navigation::isSidebar($subPage)): ?>
<?php if ($this->navigation()->accept($subPage) && MrBlue_Helper_Navigation::isAccessable($subPage, 'sidebar')): ?>
<li<?php if($subPage->isActive()): ?> class="active"<?php endif; ?>>
<a href="<?php echo $subPage->href; ?>" title="<?= $subPage->label; ?>"><?php if(isset($subPage->icon)): ?><span class="<?= $subPage->icon; ?> icon"></span> <?php endif; ?><?= $subPage->label; ?></a>
</li>
<?php endif; ?>
<?php endif; ?>
<?php endforeach; ?>
</li>
<?php else: ?>
<li<?php if($page->isActive()): ?> class="active"<?php endif; ?>>
<a href="<?php echo $page->href; ?>" title="<?= $page->label; ?>"><?php if(isset($page->icon)): ?><span class="<?= $page->icon; ?> icon"></span> <?php endif; ?><?= $page->label; ?></a>
</li>
<?php endif; ?>
<?php endif; ?>
<?php endif; ?>
<?php endforeach; ?>
</ul>
<?php endif; ?>
我也有 menu_top.phtml 文件 - 它类似于 menu_left.phtml 并具有来自 MrBlue_Helper_Navigation 的自己的方法。自己思考和做;)