带Zend\Navigation\Navigation的ZF2动态菜单

str*_*sly 5 php navigation menu dynamic zend-framework2

我想问你这个问题.使用Zend\Navigation\Navigation创建动态菜单需要什么?

在ZF1中,我做了这样的:

$container = new Zend_Navigation();
$pages = array(
array(
    'label'  => 'Save',
    'action' => 'save',
),
array(
    'label'  => 'Delete',
    'action' => 'delete',
),
);
// add two pages
$container->addPages($pages);
Run Code Online (Sandbox Code Playgroud)

然后在视图中:

$this->navigation()->menu();
Run Code Online (Sandbox Code Playgroud)

但在ZF2中,页面正在从配置中获取.现在我创建\ config\autoload \nav.global.php并在这里创建页面数组.但我需要在方法中做页面数组并将其发送到导航助手,但我不知道如何((

我试图在我的控制器中执行此操作:

use Zend\Navigation\Navigation;
$pages =array(
        // All navigation-related configuration is collected in the 'navigation' key
        'navigation' => array(
            // The DefaultNavigationFactory we configured in (1) uses 'default' as the sitemap key
            'default' => array(
                // And finally, here is where we define our page hierarchy
                'account' => array(
                    'label' => 'faq',
                    'route' => 'faq',
                    'pages' => array(
                        'news' => array(
                            'label' => 'news',
                            'route' => 'news',
                            ),
                            'manual' => array(
                            'label' => 'manual',
                            'route' => 'manual',
                            ),               
                    ),
                ),
            ),
        ),
    );
$Menu = new Navigation($pages);
Run Code Online (Sandbox Code Playgroud)

然后在视图中:

$this->Menu()->menu();
Run Code Online (Sandbox Code Playgroud)

但我犯了很多错误......

我想你明白我的问题.请帮忙.对不起我的英语不好.

Ere*_*ite 0

你需要这样做

在控制器中

$pages = new \Zend\Navigation\Page\Mvc(array(
        'pages'=>
            array(
                'album' => array(
                    'label' => 'Album3',
                    'controller' => 'album',
                    'action' => 'edit',
                    'params' => array('id'=>2),
                    'route' => 'album/default',
                )
            )
));

$navigation = new \Zend\Navigation\Navigation();
$serviceLocator = $this->getServiceLocator()->get('Application');
$routeMatch  = $serviceLocator->getMvcEvent()->getRouteMatch();
$router      = $serviceLocator->getMvcEvent()->getRouter();
$pages->setRouteMatch($routeMatch);
$pages->setDefaultRouter($router);

$navigation->addPage($pages);
Run Code Online (Sandbox Code Playgroud)

视野中

<?php echo $this->navigation($this->navigation)->menu() ?>
Run Code Online (Sandbox Code Playgroud)