如何扩展Zend Navigation Menu View Helper?

Son*_*nny 10 php zend-framework zend-view zend-navigation

我需要改变输出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
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑澄清

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

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

$navTable = new Default_Model_Site_DbTable_Navigation();
$view = $this->getResource('view');
$view->navigation(new Zend_Navigation($navTable->getNavigation()));
Run Code Online (Sandbox Code Playgroud)

在我的布局中渲染菜单:

echo $this->navigation()->menu();
Run Code Online (Sandbox Code Playgroud)

我通过如下重命名来完成它的工作,但我不清楚为什么我不能重载/覆盖_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
    }
}
Run Code Online (Sandbox Code Playgroud)

Key*_*ana 3

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


echo $this->navigation()->myMenu(); // name of your class
Run Code Online (Sandbox Code Playgroud)

来自:让 Zend_Navigation 菜单与 jQuery 的 Fisheye 一起使用

编辑

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

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