如何扩展Zend Navigation Menu View Helper?

时间:2010-03-02 16:19:45

标签: 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
    }
}

编辑澄清

我想更改<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. 在布局
  4. 中调用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
        }
    }
    

3 个答案:

答案 0 :(得分:3)

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


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

来自:Getting Zend_Navigation menu to work with jQuery's Fisheye

修改

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

但为什么这不是菜单类的延伸?

答案 1 :(得分:1)

对于任何可能需要答案的人,我发现了一种更好的方式,也许是预期的方式。

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

E.g。

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中的工作方式(名称不应该碰撞)。

答案 2 :(得分: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()。