如何在视图助手中获取导航配置


How do I get navigation configs into my view helper?

使用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();
    }
}

这对我来说真的很好,因为我可以在我的观点中做echo $this->navbar('navigation');

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

...
    'navigation' => array(
        'default' => array(
            array(
                'label' => 'eeee',
                'route' => 'link',
            ),
            array(
                'label' => 'sdlfkj',
                'route' => 'link',
            ),
            array(
                'label' => 'sdlkfj',
                'route' => 'link',
            ),
        ),
        'test' => array(
            array(
                'label' => 'aaaa',
                'route' => 'link',
            ),
            array(
                'label' => 'bbbbb',
                'route' => 'link',
            ),
        ),
    ), 
...

如何将导航下的"测试"配置拉入视图帮助程序而不是默认配置?

您正在加载另一个导航"树"。这个树必须独立设置,不幸的是,这需要几个步骤。我之前已经在这个答案中解释了如何解决这个问题。

基本上,您必须创建第二个导航工厂,将工厂链接到导航服务名称,并在视图助手中使用该服务名称。