我的一个模块中的导航视图助手正在破坏Applicationmoduleslayout.phtml中的导航栏


The navigation view helper in one of my modules is breaking the navbar in the Application modules layout.phtml

使用zf2和默认的骨架应用程序,我在一个模块中有一个视图帮助器,我用它来为模块呈现子控件栏。它看起来像这样:

class Navbar extends AbstractHelper implements ServiceLocatorAwareInterface {
    public function setServiceLocator(ServiceLocatorInterface $serviceLocator) {
        $this->serviceLocator = $serviceLocator;
        return $this;  
    }
    public function getServiceLocator() {
        return $this->serviceLocator;
    }
    public function __invoke($container) {
        $partial = array('partial/subNav.phtml','thisMeansNothing');
        //github.com/zendframework/zf2/issues/3457
        $navigation = $this->getServiceLocator()->get('navigation');
        $navigation($container)->menu()->setPartial($partial);
        return $navigation->menu()->render();
    }
}

在模块的module.config.php中,我有这样的:

...
    'navigation' => array(
        'subnav' => array(
            array(
                'label' => 'aaa',
                'route' => 'link',
            ),
            array(
                'label' => 'bbb',
                'route' => 'link',
            ),
            array(
                'label' => 'ccc',
                'route' => 'link',
            ),
        ),
...

按照这些步骤,我扩展了DefaultNavigationFactory以加载该模块的"subgov"配置,并将其注册在module.config.php中:

'service_manager' => array(
    'factories' => array(
        'subnav_navigation' => 
            'myModule'Navigation'Service'SubNavNavigationFactory'

这对我来说非常有效,因为我可以在模块中的视图中执行echo $this->navbar('subnav_navigation');

这很好,但现在我想在应用程序模块中修改我的站点导航栏。我不需要视图助手,因为我可以在layout''layout.phtml中创建它,如果默认的话。

我修改了应用程序模块的module.config.php,如下所示:

'navigation' => array(
    'default' => array(
        array(
            'label' => 'wwww',
            'route' => 'wwww',
        ),
        array(
            'label' => 'qqq',
            'route' => 'qqq',
            'pages' => array(
                array(
                    'label' => 'sdf',
                    'route' => 'sdf',
                ), ...

在layout.phtml中,我这样做:

...
<div class="collapse navbar-collapse">
    <ul class="nav navbar-nav">
        <!-- Load from module.config.php -->
        <?php 
            echo $this->navigation('navigation')
                ->menu()
                ->setMinDepth(0)
                ->setMaxDepth(0)
                ->setUlClass('nav navbar-nav');
        ?>...

在主页面上,它看起来还可以,但当我出于某种原因导航到另一个模块时,该模块正在使用自己的视图助手来渲染layout.phtml中的导航栏,并将其搞砸。在模块中,layout.phtml的$this->navigation('navigation')部分是用模块的视图助手中的部分填充的,但它仍然从应用模块module.config.php中获得正确的配置。它为什么要这样做,我如何防止模块使用它的视图助手在layout.ptml中渲染导航栏?

导航助手会记住它们的上一个状态,其中包括菜单、使用的部分和最小/最大深度等。

如果您多次调用帮助程序,则必须重置这些值,否则将再次应用这些值。在您的情况下,如果分部是唯一受影响的值,请使用值为nullsetPartial()来阻止辅助对象再次使用它。

    <!-- Load from module.config.php -->
    <?php 
        echo $this->navigation('navigation')
            ->menu()
            ->setMinDepth(0)
            ->setMaxDepth(0)
            ->setUlClass('nav navbar-nav')
            ->setPartial(null); // reset partial 
    ?>