Navigation ZF2导航帮助程序getHref无法执行错误
我正在尝试将导航从ZF1迁移到ZF2。我从数据库中提取数据,构建多维数组,并将其传递给导航容器 我可以从view helper访问导航容器,因此所有内容都应该正确注册。但是,当我试图在视图中生成菜单时,我遇到以下错误:Navigation ZF2导航帮助程序getHref无法执行错误,navigation,zend-framework2,Navigation,Zend Framework2,我正在尝试将导航从ZF1迁移到ZF2。我从数据库中提取数据,构建多维数组,并将其传递给导航容器 我可以从view helper访问导航容器,因此所有内容都应该正确注册。但是,当我试图在视图中生成菜单时,我遇到以下错误: Fatal error: Zend\Navigation\Exception\DomainException: Zend\Navigation\Page\Mvc::getHref cannot execute as no Zend\Mvc\Router\RouteStackInt
Fatal error: Zend\Navigation\Exception\DomainException: Zend\Navigation\Page\Mvc::getHref cannot execute as no Zend\Mvc\Router\RouteStackInterface instance is composed
通过手动使用URL帮助器,我可以构建正确的URL,所以路由定义正确
请问哪里有问题?我应该检查什么
更新:添加代码
创建多阵列:
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, '/') );
}
}
}
获取容器:
public function buildNavigation() {
$this->buildTree();
return new \Zend\Navigation\Navigation($this->_configArray);
}
设置导航。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();
},
),
),
..并在layout.phtml中
<?php echo $this->navigation('navigation')->menu(); ?>
添加了代码,请选中查看Zend\Navigation\Service\AbstractNavigationFactory,特别是这里的preparePages方法->如何注入页面构建路由所需的组件。您最好按照下面的答案->谢谢扩展抽象工厂类。在我看来,这是在做同样的事情:你的方法只是不同的实现相同。最后,您认为我需要显式地将路由器注入导航吗?这就是这里的问题所在?最后,我通过使用您的方法获得了菜单,所以谢谢:无论如何,如果有人能解释我的方法不起作用的原因,我将不胜感激-只是为了更好地理解ZF2。