Zend_Navigation多个菜单

时间:2013-08-05 05:12:49

标签: zend-framework navigation

如何在网站上有多个菜单?

现在我有一个顶级菜单(可通过ACL权限访问的项目),但如何创建第二个侧面菜单? 在多个页面上,我将有多个sidemenu。

我认为应该工作的第一种方法是创建一个包含两个新属性的页面树文件 - “position”(格式为:“top”或“top | sidemenu”)和“where2display”(格式为:“控制器:动作|控制器:行动“)。 并在top.phtml或sidemenu.phtml中检查位置是否为top / sidemenu,并且菜单项可以显示为指定控制器&动作。

1 个答案:

答案 0 :(得分:0)

我按照我的说法做了:)这个功能很简单,很有弹性。

navigation.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:*',
            ),

查看助手,它将有助于爆炸自定义导航属性:

<?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文件 - 它与使用MrBlue_Helper_Navigation自己的方法的menu_left.phtml类似。想想并亲自去做​​;)