-1

在我ZF2应用程序,我有这个导航视图脚本:将ZF2移植到ZF3:如何修改导航视图?

$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; ?> 

由于getServiceLocator()现在已经过时,我需要另一种方式来获取有关当前的路线进入导航视图脚本的信息。

这一观点得到脚本设置部分,并呼吁在layout.phtml

echo $this->navigation('navigation')->menu()->setPartial('navigation/default'); 

所以我得到了主意,通过通过模板变量需要的信息。我创建了一个在Module#onDispatch(...)

public function onDispatch(MvcEvent $mvcEvent) 
{ 
    $viewModel = $mvcEvent->getApplication()->getMvcEvent()->getViewModel(); 
    $routeMatch = $mvcEvent->getRouteMatch(); 
    $viewModel->currentRoute = $routeMatch instanceof RouteMatch 
     ? $routeMatch->getMatchedRouteName() : null 
    ; 
} 

现在我可以在layout.phtml访问它。但我没有找到一种方法将它传递给导航视图脚本,因为Zend\View\Helper\Navigation\Menu#setPartial(...)`不接受变量的任何参数。

如何将变量(特别是关于当前路径的信息)传递给Zend Framework 3中的导航视图?

回答

-1

使用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', 
         ], 
        ], 
       ], 
      ], 
     ], 
    ], 
], 

现在,我们将定义下的顶级键navigation我们的导航配置并启用Zend的导航。你可以把这个代码到你config/autoload/global.php

根据上述路由配置我们的导航结构会像下面

// 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从导航视图助手将上面的配置导入到视图脚本中。看下面

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

就是这样!

下面显示了如何限制路由到用户,并通过导航帮助器的setter方法设置ACL对象或用户角色对象。这里$this->loggedInUser$this->acl$this->userRole已经注入到视图脚本从而

<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> 

希望这会帮助你!

+0

感谢您的回答,但它已经在工作。问题在于''active''属性的设置,并且通常会将变量传递到导航视图脚本中。 – automatix

+0

您随时欢迎您!很高兴已经做到了! – unclexo

+0

再次感谢您的回答!它解决了这个问题。 – automatix