zend-framework - Zend Framework - 定义导航菜单的正确方法

标签 zend-framework zend-navigation

我正在使用一个模板,其中有两个不同的导航菜单。

  1. Main navigation menu. ( fixed )

  2. Action navigation menu, contains elements such as back, save, delete etc. this navigation elements changes according to the controllers being called. while it may exist for some controller and for some it doesn't



我正在使用布局,并且我已将所有模板代码放在我的 default.phtml 中布局文件。我面临的问题是 Action 导航菜单 .因为这个菜单的 html 代码位于 default.phtml我需要根据被调用的 Controller 更改它的内容。

我不确定这是否是正确的做法。但在我的default.phtml我正在检查 Controller 名称并相应地显示菜单。这是我正在使用的代码。
<?php if(Zend_Controller_Front::getInstance()->getRequest()->getControllerName() == 'item'): ?>
    <!-- Action Navigation Menu -->
    <div class="statsRow">
        <div class="wrapper">
            <a href="#"><img src="/images/icons/dark/add.png" alt="" class="icon"><span>New item</span></a>
            <a href="#"><img src="/images/icons/dark/cd.png" alt="" class="icon"><span>Publish / Unpublish item</span></a>
            <a href="#"><img src="/images/icons/dark/trash.png" alt="" class="icon"><span>Delete item</span></a>
        </div>
    </div>
    <div class="line"></div>
<?php endif; ?>

这样做可以吗?还是我有更好的方法来做到这一点?

最佳答案

看起来您的操作导航菜单实际上就像一个子菜单,其中每个操作都是子页面的一部分。做这种事情的一个好方法是拥有两个 Zend_Navigation 实例,例如,您可以将它们存储在注册表中。然后,在您的 Controller 中,您可以调用 操作助手 preDispatch()像这样的方法:

// in each controller where you want your "action navigation menu"
public function preDispatch()
{
    $this->_helper->navigation()->renderActionNavigation();
}

当然,只有需要这个 Action 导航菜单的 Controller 才有这个方法。
这个 Action Helper 基本上会获取当前 View 对象,创建一个占位符并渲染一个部分,如下所示:
// in your library/My/Controller/Action/Helper
class My_Controller_Action_Helper_Navigation extends Zend_Controller_Action_Helper_Abstract
{
    private $_view = null;

    public function direct()
    {
        $this->_view = $view = Zend_Layout::getMvcInstance()->getView();
        $this->_view->placeholder('action-navigation');
        return $this;
    }

    public function renderActionNavigation()
    {
        $this->_view->render('partials/_action-navigation.phtml');
    }
}

如果您不使用库,只需将此代码放入/views/helpers/并重命名类 Zend_View_Helper_Navigation .

然后部分将负责使用占位符呈现您的子菜单:
// in /view/scripts/partials/_action-navigation.phtml
<?php $this->placeholder('action-navigation')->captureStart() ?>
<?php $options = array('onlyActiveBranch' => true); ?>
<?= $this->navigation()->menu()->renderMenu(Zend_Registry::get('nav.action-navigation'), $options);
// here I assume that you've stored your Navigation container in the registry ?>
<?php $this->placeholder('action-navigation')->captureEnd() ?>

此外,使用 navigation view helper method renderMenu() 渲染菜单有一个选项onlyActiveBranch设置为 true将允许您到 只渲染事件分支 ,其中每个分支将 对应您的 Controller .

最后,在您的布局中,您将拥有以下内容:
 // in your layout file (usually named layout.phtml)
<?= $this->placeholder('action-navigation'); ?>

如果您选择在注册表中注册 Zend_Navigation 容器,这可以在 Bootstrap 中以这种方式完成:
// in your bootstrap.php
protected function _initNavigation()
{
    $config = new Zend_Config_Xml(APPLICATION_PATH . '/configs/navigation.xml', 'nav');
    $container = new Zend_Navigation($config);
    Zend_Registry::set('nav.action-navigation', $container);
}

有关容器的更多信息,refer to this page .另外,如果您不熟悉占位符,here is a good practical example关于如何使用它们。

关于zend-framework - Zend Framework - 定义导航菜单的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10062656/

相关文章:

zend-framework - 在 NetBeans 上使用 Zend Framework 的问题

php - 用于从 SET 列中获取最常用项目的 SQL 查询

php - 如何访问 ZF2 Field Set 中的数据库适配器?

zend-framework - Zend 导航面包屑绕过 ACL

php - 赋予 Zend 导航页面多个 ACL 权限

zend-framework - 是否可以将 zend acl 中的权限设置为操作级别而不是 Controller 级别

zend-framework - 如果父项和子项具有相同的 URI 并使用 Zend_Navigation,如何在菜单中呈现事件的父项和子项?

php - Zend 框架 2 数据库 : Make a part of the query negative using Predicate objects

php - 如何使用 Zend 服务亚马逊?

php - Zend_Translate - Zend_Navigation 和 Routing 组合问题!