代码之家  ›  专栏  ›  技术社区  ›  Sonny

如何扩展Zend导航菜单视图帮助程序?

  •  10
  • Sonny  · 技术社区  · 15 年前

    我需要改变 Zend_View_Helper_Navigation_Menu . 我找到了两个需要修改的函数,我知道如何进行所需的更改。我不知道如何让导航对象使用我的视图帮助器而不是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 和缩进。对于菜单视图脚本,没有这样的选项,这就是为什么我必须扩展它的原因。

    在我的引导程序中初始化导航对象:

    $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
        }
    }
    
    3 回复  |  直到 15 年前
        1
  •  3
  •   Community CDub    8 年前
       $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

    编辑

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

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

        2
  •  1
  •   FDIM    12 年前

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

    您唯一要做的就是创建自己的自定义视图助手,扩展“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中的工作方式(名称不应该冲突)。

        3
  •  0
  •   Layke    15 年前

    你编辑了你的帖子吗? 我的回答好像和你的问题完全无关?


    如果你说你需要改变的事情会更容易。现在你的问题有点令人困惑。

    我假设您希望在 已经 创建了您的导航。如果你能做到的话 之前 你创造了它,然后更容易。下面这一点有点令人困惑,因为您通常会在手前更改选项。

    // 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_对象。仅当您在页面上使用多个导航时才使用该选项。否则,只能使用render()而不是rendermenu()。