0

I'm trying to migrate my navigation from ZF1 to ZF2. I've did extract from DB, built multidimensional array, passed that to Navigation container.

I can access Navigation container from view helper, so all should be registered correctly. However, when I'm trying to generate menu in the view, I'm getting following error:

Fatal error: Zend\Navigation\Exception\DomainException: Zend\Navigation\Page\Mvc::getHref cannot execute as no Zend\Mvc\Router\RouteStackInterface instance is composed

By using URL helper manually, I can build correct URL, so route is defined OK.

Where is the problem please ? What should I check ?

UPDATE: adding code

Creating multi array:

protected function buildTree() {

            $references = array();

            foreach ( $this->_flatArray as &$node) {

                // Add the node to our associative array using it's ID as key
                $references[$node['id']] = &$node;

                // Add empty placeholder for children
                $node['pages'] = array();

                // zvýrazníme, ak treba
                if( $node['zvyraznena'] == 1 ) {
                    $zvyraznenie = self::_trieda_zvyraznovania;
                } else {
                    $zvyraznenie = NULL;
                }


                // It it's a root node, we add it directly to the tree
                if ( $node['parentId'] == self::_rootID) {

                    $this->_configArray[$node['id']] = &$node;
                    $this->_configArray[$node['id']]['class'] = $zvyraznenie;
                    $this->_configArray[$node['id']]['full_path'] = '';
                    $this->_configArray[$node['id']]['route'] = $this->_router;

                    $this->_configArray[$node['id']]['params'] = array('nodeID' => $node['id'], 'full_path' => $node['url'] );

                } else {
                    // It was not a root node, add this node as a reference in the parent.

                    if( isset($references[$node['parentId']]['pages'][$node['id']]['class']) ) $parent_seo = $references[$node['parentId']]['pages'][$node['id']]['class'] . $parent_seo . '/';

                    $references[$node['parentId']]['pages'][$node['id']] = &$node;
                    $references[$node['parentId']]['pages'][$node['id']]['class'] =  $zvyraznenie;
                    $references[$node['parentId']]['pages'][$node['id']]['full_path'] = $full_path = $references[$node['parentId']]['full_path']  . $node['url'] . '/';
                    $references[$node['parentId']]['pages'][$node['id']]['route'] = $this->_router;

                    $references[$node['parentId']]['pages'][$node['id']]['params'] = array('nodeID' => $node['id'], 'full_path' => rtrim($full_path, '/') );
                }

            }

        }

Getting container:

public function buildNavigation() {

            $this->buildTree();
            return new \Zend\Navigation\Navigation($this->_configArray);
        }

setup nav. container and helper in global.php

'service_manager' => array(
        'factories' => array(
            'Zend\Db\Adapter\Adapter' => 'Zend\Db\Adapter\AdapterServiceFactory',
            'navigation' => function() { // navigácia z DB do Service Managera

                $navigation = new \Cls\Navigacia();
                return $navigation->buildNavigation();
            }, 
        ),
    ),

..and in the layout.phtml

<?php echo $this->navigation('navigation')->menu(); ?>
Ivan
  • 315
  • 1
  • 3
  • 16
  • code added, please check – Ivan May 07 '14 at 10:13
  • Take a look at `Zend\Navigation\Service\AbstractNavigationFactory` and in particular at how the `preparePages()` method here -> https://github.com/zendframework/zf2/blob/master/library/Zend/Navigation/Service/AbstractNavigationFactory.php#L73-L91 injects the components needed for a page to build a route. You'd be better off extending the abstract factory class as described in the answer here -> http://stackoverflow.com/questions/16622179/zf2-injecting-pages-to-navigation-before-controller-is-called – Crisp May 07 '14 at 10:25
  • Thanks. Seems to me, that it is doing the same thing :) You approach is just different implementation of the same. Finally, do you think, that I need to explicitly inject router to the Navigation ? That's the issue here ? – Ivan May 07 '14 at 11:15
  • Finally, I've managed to get Menu by using your approach, so thanks :) Anyway, If someone can explain why my approach is not working, I'll appreciate that - just for better ZF2 understanding. – Ivan May 07 '14 at 12:19

0 Answers0