如何在zf2中设置2个导航? [英] How to set up 2 navigations in zf2?

查看:102
本文介绍了如何在zf2中设置2个导航?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用于主导航的配置链接,第二个配置只有1个

I have a config for main navigation link and the second just with 1 link. The main navigation is working fine, but when I try setting up a second navigation bar in module like this:

$config = $e->getApplication()->getServiceManager()->get('config');
$navigation = new \Zend\Navigation\Navigation($config['navigation_footer']);
$e->getApplication()->getServiceManager()
    ->setService('new_navigation', $navigation);`

在视图中渲染时出现错误:

I get an error when I render it in the view:

致命错误:Zend \ Navigation \ Exception \ DomainException:Zend \ Navigation \ Page \ Mvc :: getHref无法执行,因为/home/cawa/www/sp-app/中没有Zend \ Mvc \ Router \ RouteStackInterface实例471行上的vendor/zendframework/zendframework/library/Zend/View/Helper/Navigation/AbstractHelper.php

Fatal error: Zend\Navigation\Exception\DomainException: Zend\Navigation\Page\Mvc::getHref cannot execute as no Zend\Mvc\Router\RouteStackInterface instance is composed in /home/cawa/www/sp-app/vendor/zendframework/zendframework/library/Zend/View/Helper/Navigation/AbstractHelper.php on line 471

推荐答案

问题是缺少路由器(或更确切地说,是Zend\Mvc\Router\RouteStackInterface).路由堆栈是路由的集合,可以使用路由名称将其转换为url.基本上,它接受路由名称并为您创建一个网址:

The problem is a missing Router (or to be more precise, a Zend\Mvc\Router\RouteStackInterface). A route stack is a collection of routes and can use a route name to turn that into an url. Basically it accepts a route name and creates an url for you:

$url = $routeStack->assemble('my/route');

这也在Zend\Navigation的MVC页面内发生.该页面具有一个route参数,并且当有可用的路由器时,该页面将组装其自己的url(或以Zend\Navigation的形式表示为href).如果不提供路由器,则无法组装路由,因此会引发异常.

This happens inside the MVC Pages of Zend\Navigation too. The page has a route parameter and when there is a router available, the page assembles it's own url (or in Zend\Navigation terms, an href). If you do not provide the router, it cannot assemble the route and thus throws an exception.

您必须在导航的每个页面中注入路由器:

You must inject the router in every page of the navigation:

$navigation = new Navigation($config);
$router     = $serviceLocator->get('router');

function injectRouter($navigation, $router) {
  foreach ($navigation->getPages() as $page) {
    if ($page instanceof MvcPage) {
      $page->setRouter($router);
    }

    if ($page->hasPages()) {
      injectRouter($page, $router);
    }
  }
}

如您所见,它是一个递归函数,将路由器插入每个页面.乏味!因此,有一家工厂可以为您执行此操作.要完成此操作,只需执行四个简单步骤.

As you see it is a recursive function, injecting the router into every page. Tedious! Therefore there is a factory to do this for you. There are four simple steps to make this happen.

第一个步骤

首先将导航配置放入模块配置中.就像有default导航一样,您可以创建第二个secondary.

Put the navigation configuration in your module configuration first. Just as you have a default navigation, you can create a second one secondary.

'navigation' => array(
    'secondary' => array(
        'page-1' => array(
            'label' => 'First page',
            'route' => 'route-1'
        ),
        'page-2' => array(
            'label' => 'Second page',
            'route' => 'route-2'
        ),
    ),
),

您有通往第一页(route-1)和第二页(route-2)的路线.

You have routes to your first page (route-1) and second page (route-2).

第2步

工厂将其转换为导航对象结构,您需要首先为其创建一个类.在您的MyModule/Navigation/Service目录中创建文件SecondaryNavigationFactory.php.

A factory will convert this into a navigation object structure, you need to create a class for that first. Create a file SecondaryNavigationFactory.php in your MyModule/Navigation/Service directory.

namespace MyModule\Navigation\Service;

use Zend\Navigation\Service\DefaultNavigationFactory;

class SecondaryNavigationFactory extends DefaultNavigationFactory
{
    protected function getName()
    {
        return 'secondary';
    }
}

请参见我在此处输入名称secondary,该名称与您的导航键相同.

See I put the name secondary here, which is the same as your navigation key.

第3步

您必须将此工厂注册到服务经理.然后,工厂可以完成工作并将配置文件转换为Zend\Navigation对象.您可以在module.config.php中执行此操作:

You must register this factory to the service manager. Then the factory can do it's work and turn the configuration file into a Zend\Navigation object. You can do this in your module.config.php:

'service_manager' => array(
    'factories' => array(
        'secondary_navigation' => 'MyModule\Navigation\Service\SecondaryNavigationFactory'
    ),
)

请参见我在此处提供了服务secondary_navigation,然后工厂将返回一个Zend\Navigation实例.如果现在执行$sm->get('secondary_navigation'),您将看到这是一个Zend\Navigation\Navigation对象.

See I made a service secondary_navigation here, where the factory will return a Zend\Navigation instance then. If you do now $sm->get('secondary_navigation') you will see that is a Zend\Navigation\Navigation object.

第4步

告诉视图助手使用此导航,而不是默认导航.导航视图帮助器接受导航"参数,您可以在其中声明所需的导航.在这种情况下,服务管理器有一项服务secondary_navigation,这就是我们需要的服务.

Tell the view helper to use this navigation and not the default one. The navigation view helper accepts a "navigation" parameter where you can state which navigation you want. In this case, the service manager has a service secondary_navigation and that is the one we need.

<?= $this->navigation('secondary_navigation')->menu() ?>

现在,您将在此视图帮助器中使用导航secondary.

Now you will have the navigation secondary used in this view helper.

这篇关于如何在zf2中设置2个导航?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆