Navigation 将ZF2迁移到ZF3:如何调整导航视图?

Navigation 将ZF2迁移到ZF3:如何调整导航视图?,navigation,migration,zend-view,zend-navigation,zend-framework3,Navigation,Migration,Zend View,Zend Navigation,Zend Framework3,在我的ZF2应用程序中,我有一个导航视图脚本: $routeMatch = $this->getHelperPluginManager() ->getServiceLocator() ->get('Application') ->getMvcEvent() ->getRouteMatch(); $currentRoute = $routeMatch instanceof \Zend\Mvc\Router\RouteMatch ? $r

在我的ZF2应用程序中,我有一个导航视图脚本:

$routeMatch = $this->getHelperPluginManager()
    ->getServiceLocator()
    ->get('Application')
    ->getMvcEvent()
    ->getRouteMatch();
$currentRoute = $routeMatch instanceof \Zend\Mvc\Router\RouteMatch ? $routeMatch->getMatchedRouteName() : null;
?>
<?php foreach ($this->container as $page) : ?>
<li <?php echo $currentRoute == $page->getRoute() ? ' class="active"' : null;?>>
    <?php echo $this->navigation()->menu()->htmlify($page, true, false) . PHP_EOL; ?>
</li>
<?php endforeach; ?>
所以我想通过一个模板变量来传递需求信息。我在
模块#onDispatch(…)
中创建了一个:

现在我可以在
layout.phtml
中访问它。但是我没有找到将其传递到导航视图脚本的方法,因为`不接受变量的任何参数


如何将变量(特别是关于当前路线的信息)传递到Zend Framework 3中的导航视图中?使用
DefaultNavigationFactory
最简单的方法

假设我们有如下几条路线

'router' => [
    'routes' => [
        'world' => [
            'type'    => Literal::class,
            'options' => [
                'route'    => '/world',
                'defaults' => [
                    'controller' => Controller\IndexController::class,
                    'action'     => 'index',
                ],
            ],
            'may_terminate' => true,
            'child_routes' => [
                'country' => [
                    'type' => Literal::class,
                    'options' => [
                        'route' => '/country',
                        'defaults' => [
                            'controller' => Controller\WorldController::class,
                            'action' => 'country',
                        ],
                    ],
                ],
            ],
        ],
    ],
],
// configure the navigation
'navigation' => [
    'default' => [
        [
            'label' => 'Home',
            'route' => 'home',
        ],
        [
            'label' => 'World',
            'route' => 'world', // this should be the route name not route pattern like '/world'
            'pages' => [
                [
                    'label' => 'Country',
                    'route' => 'world/country',
                ],
            ],
        ],
    ],
],

// Register the navigation
'service_manager' => [
    'factories' => [
        'navigation' => Zend\Navigation\Service\DefaultNavigationFactory::class,
    ],
],
现在,我们将在顶级键
navigation
下定义导航配置,并启用zend导航。您可以将此代码放入
config/autoload/global.php

根据上述路线配置,我们的导航配置如下

'router' => [
    'routes' => [
        'world' => [
            'type'    => Literal::class,
            'options' => [
                'route'    => '/world',
                'defaults' => [
                    'controller' => Controller\IndexController::class,
                    'action'     => 'index',
                ],
            ],
            'may_terminate' => true,
            'child_routes' => [
                'country' => [
                    'type' => Literal::class,
                    'options' => [
                        'route' => '/country',
                        'defaults' => [
                            'controller' => Controller\WorldController::class,
                            'action' => 'country',
                        ],
                    ],
                ],
            ],
        ],
    ],
],
// configure the navigation
'navigation' => [
    'default' => [
        [
            'label' => 'Home',
            'route' => 'home',
        ],
        [
            'label' => 'World',
            'route' => 'world', // this should be the route name not route pattern like '/world'
            'pages' => [
                [
                    'label' => 'Country',
                    'route' => 'world/country',
                ],
            ],
        ],
    ],
],

// Register the navigation
'service_manager' => [
    'factories' => [
        'navigation' => Zend\Navigation\Service\DefaultNavigationFactory::class,
    ],
],
现在,我们可以使用上面配置中的top键
navigation
将导航视图助手调用到视图脚本中。见下文

<div class="collapse navbar-collapse">
    <?php 
        echo $this->navigation('navigation')
            ->menu()
            ->setUlClass('nav navbar-nav')
            ->renderMenu(); 
        ?>
</div>

希望这对你有帮助

谢谢你的回答,但它已经起作用了。问题在于设置活动的
属性,通常将变量传递到导航视图脚本中。再次感谢您的回答!它解决了这个问题。
<div class="collapse navbar-collapse" id="main-menu">

    <?php if ($this->loggedInUser !== null) : ?>
        <?php $this->navigation('navigation')
                ->findBy('route', 'hello')
                ->setParams(array('username' => $this->loggedInUser->getUsername())); ?> // If you need to pass any params

        <?php $this->navigation('navigation')
                ->findBy('route', 'world'); ?>

    <?php endif; ?>

    <?php 
        echo $this->navigation('navigation')
            ->menu()
            ->setUlClass('nav navbar-nav')
            ->setAcl($this->acl) // ACL object
            ->setRole($this->userRole) // Role Object
            ->renderMenu(); 
    ?>

</div>