Zend Framework - multiplate navigation blocks

前端 未结 3 1467
醉梦人生
醉梦人生 2020-12-05 12:04

I want to use the navigation helper to build my navigation menus using Acl. The Acl part I have working fine.

I now want to be able to display a few different types

相关标签:
3条回答
  • 2020-12-05 12:57

    I just ran into this issue of needing multiple navigations and in the process discovered your problem and it is actually a bug in the Zend_View_Helper_Navigation_HelperAbstract.

    Line 516:

    public function __toString()
    {
        try {
            return $this->render();
        } catch (Exception $e) {
            $msg = get_class($e) . ': ' . $e->getMessage();
            trigger_error($msg, E_USER_ERROR);
            return '';
        }
    }
    

    The problem here is that if you don't explicitly call $this->navigation->render($container) or a magic method like $this->navigation()->menu($container) then the call to render ends up not getting a container passed to it.

    This in turn causes the default view helper for navigation, which is menu, to be pulled from the registry (in which case it will use the last given container), or instantiated on the spot (which causes there to be no container).

    This is my simple fix which calls getContainer on __toString.

    Line 516:

    public function __toString()
    {
        try {
            return $this->render($this->getContainer());
        } catch (Exception $e) {
            $msg = get_class($e) . ': ' . $e->getMessage();
            trigger_error($msg, E_USER_ERROR);
            return '';
        }
    }
    

    Looking through all of the helper files related to Navigation it is clear that the intention was to call getContainer. It is also clear that this isn't an issue if you call the menu view helper either directly or via the navigation magic method.

    Once you change that line above you should be able to call $this->navigation($container) and render multiple navigations without having to resort to calling the menu helper directly.

    0 讨论(0)
  • 2020-12-05 13:01

    or you can shorten syntax

    $this->menu($this->menu1);
    
    0 讨论(0)
  • 2020-12-05 13:05

    I have had this exact same issue. I just create multiple instances of Zend_Navigation_Container in my controllers for each of the menus I need, pass them to the view and then render them by passing the objects directly to the menu render method. As follows:

    In the controller:

    $this->view->menu1 = new Zend_Navigation_Container();
    $this->view->menu2 = new Zend_Navigation_Container();
    

    In the view:

    $this->navigation()->menu()->renderMenu($this->menu1);
    $this->navigation()->menu()->renderMenu($this->menu2);
    

    You could even customise each one (by inserting method calls after the initial menu() call:

    $this->navigation()->menu()->setUlClass('my_first_menu')->renderMenu($this->menu1);
    $this->navigation()->menu()->setUlClass('my_second_menu')->renderMenu($this->menu2);
    
    0 讨论(0)
提交回复
热议问题