在我的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; ?>
由于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的导航视图中?
答案 0 :(得分:-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对象或用户Role对象。此处$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>
希望这会对你有帮助!