10

我需要更改Zend_View_Helper_Navigation_Menu. 我找到了需要修改的两个函数,并且我知道如何进行所需的更改。我不知道如何让 Navigation 对象使用我的视图助手而不是 Zend 。

代表我的类扩展的代码片段:

// file /library/My/View/Helper/Navigation/Menu.php
class My_View_Helper_Navigation_Menu extends Zend_View_Helper_Navigation_Menu
{
    protected function _renderDeepestMenu(Zend_Navigation_Container $container,
                                          $ulClass,
                                          $indent,
                                          $minDepth,
                                          $maxDepth)
    {
        // modified code here
    }

    protected function _renderMenu(Zend_Navigation_Container $container,
                                   $ulClass,
                                   $indent,
                                   $minDepth,
                                   $maxDepth,
                                   $onlyActive)
    {
        // modified code here
    }
}

澄清的编辑

我想更改<li>元素的类并删除EOL和缩进。菜单视图脚本没有选项可以做到这一点,这就是我必须扩展它的原因。

在我的 Bootstrap 中初始化导航对象:

$navTable = new Default_Model_Site_DbTable_Navigation();
$view = $this->getResource('view');
$view->navigation(new Zend_Navigation($navTable->getNavigation()));

在我的布局中渲染菜单:

echo $this->navigation()->menu();

解决方案

我通过如下重命名它来工作,但我不清楚为什么我不能重载/覆盖_Menu类和menu()函数。

  1. 将类名更改为My_View_Helper_Navigation_MyMenu
  2. myMenu函数添加到类 ( return parent::menu($container);)
  3. 调用echo $this->navigation()->myMenu();布局

类线框:

// file /library/My/View/Helper/Navigation/MyMenu.php
class My_View_Helper_Navigation_MyMenu extends Zend_View_Helper_Navigation_Menu
{
    public function myMenu(Zend_Navigation_Container $container = null)
    {
        return parent::menu($container);
    }

    protected function _renderDeepestMenu(Zend_Navigation_Container $container,
                                          $ulClass,
                                          $indent,
                                          $minDepth,
                                          $maxDepth)
    {
        // modified code here
    }

    protected function _renderMenu(Zend_Navigation_Container $container,
                                   $ulClass,
                                   $indent,
                                   $minDepth,
                                   $maxDepth,
                                   $onlyActive)
    {
        // modified code here
    }
}
4

3 回答 3

3
   $view->addHelperPath(
      APPLICATION_ROOT . '/library/MyApp/View/Helper/Navigation',
      'MyApp_View_Helper_'
      );


echo $this->navigation()->myMenu(); // name of your class

来自:让 Zend_Navigation 菜单与 jQuery 的 Fisheye 一起工作

编辑

抱歉,我没有看到您的解决方案,这正是我发布的内容。

但是为什么这不是菜单类的真正扩展呢?

于 2010-03-02T18:44:24.870 回答
1

对于任何可能需要答案的人,我找到了一种更好的方法,并且可能是预期的方法。

您唯一需要做的就是创建您自己的自定义视图助手,扩展“Zend_View_Helper_Navigation_HelperAbstract”并将导航视图助手的默认代理设置为您自己的。

例如

class Admin_View_Helper_NavigationMenu extends
                                     Zend_View_Helper_Navigation_HelperAbstract {

    public function render(\Zend_Navigation_Container $container = null) {
        return "Hello world!!";
    }

}

$this->view->navigation()->setDefaultProxy("navigationMenu");

(我正在更改菜单控制器操作中的默认代理,因为它已添加到操作堆栈中)

完成后,就可以在视图中使用它

<?= $this->navigation()->render(); ?>

注意:您仍然需要重命名视图助手类,但这就是 Zend 中视图助手的工作方式(名称不应该发生冲突)。

于 2013-05-23T07:52:16.770 回答
0

你编辑你的帖子了吗?现在看来我的回答与您的问题完全无关?


如果你说你需要改变什么,它会更容易。目前你的问题有点令人困惑。

我假设您想在创建导航之后编辑视图。如果您能够在创建它之前完成它,那么它就更容易了。下面的这一点有点令人困惑,因为您通常会事先更改选项。

// Get the helper navigation
$navigation = $viewRenderer->
                       view->
                       getHelper( 'navigation' )
                      ->menu()
                      ->renderMenu(
                    $YOUR_NAVIGATION_OBJECT,                                
                    array(  'minDepth' => null,
                        'maxDepth' => null,
                        'onlyActiveBranch' => false,
                        'renderParents'    => false,
                        // More options here


                    )                           

);

原谅压痕,真的很难让它几乎排成一行

请注意,我在上面使用了 $YOUR_NAVIGATION_OBJECT。仅当您在页面上使用多个导航时才使用它。否则,您只需使用 Render() 而不是 RenderMenu()。

于 2010-03-02T16:34:47.950 回答