red888
red888

Reputation: 31610

How do I get navigation configs into my view helper?

Using zf2 I have a view helper that looks like this:

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();

    }
}

This works really well for me because I can just do echo $this->navbar('navigation'); in my views.

In my module's module.config.php I have this:

...
    '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',
            ),
        ),
    ), 
...

How do I pull the 'test' config under navigation into my view helper instead of the default?

Upvotes: 0

Views: 103

Answers (1)

Jurian Sluiman
Jurian Sluiman

Reputation: 13558

You are loading another navigation 'tree'. This tree must be setup independently and unfortunately, that does take a couple of steps. I have earlier explained how to fix this in this answer.

Basically, you have to create a second navigation factory, link the factory to a navigation service name and use that service name in your view helpers.

Upvotes: 1

Related Questions