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

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

问题描述

我有一个主导航配置 link 而第二个只有 1 个 链接.主导航工作正常,但是当我尝试在模块中设置 second 导航栏时:

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 endNavigationNavigation($config['navigation_footer']);
$e->getApplication()->getServiceManager()
    ->setService('new_navigation', $navigation);`

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

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

致命错误:ZendNavigationExceptionDomainException: ZendNavigationPageMvc::getHref 无法执行,因为/home/cawa/www/sp-app/中没有 ZendMvcRouterRouteStackInterface 实例vendor/zendframework/zendframework/library/Zend/View/Helper/Navigation/AbstractHelper.php 在第 471 行

Fatal error: ZendNavigationExceptionDomainException: ZendNavigationPageMvc::getHref cannot execute as no ZendMvcRouterRouteStackInterface instance is composed in /home/cawa/www/sp-app/vendor/zendframework/zendframework/library/Zend/View/Helper/Navigation/AbstractHelper.php on line 471

推荐答案

问题是缺少路由器(或者更准确地说,是一个 ZendMvcRouterRouteStackInterface).路由堆栈是路由的集合,可以使用路由名称将其转换为 url.基本上它接受一个路由名称并为你创建一个 url:

The problem is a missing Router (or to be more precise, a ZendMvcRouterRouteStackInterface). 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');

这也发生在 ZendNavigation 的 MVC 页面中.该页面有一个 route 参数,当有一个可用的路由器时,该页面组装它自己的 url(或在 ZendNavigation 术语中,一个 href).如果不提供路由器,则无法组装路由,从而引发异常.

This happens inside the MVC Pages of ZendNavigation too. The page has a route parameter and when there is a router available, the page assembles it's own url (or in ZendNavigation 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).

第二步

工厂会将其转换为导航对象结构,您需要先为此创建一个类.在您的 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 MyModuleNavigationService;

use ZendNavigationServiceDefaultNavigationFactory;

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.

第三步

您必须将此工厂注册到服务管理器.然后工厂可以完成它的工作并将配置文件转换为 ZendNavigation 对象.您可以在您的 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 ZendNavigation object. You can do this in your module.config.php:

'service_manager' => array(
    'factories' => array(
        'secondary_navigation' => 'MyModuleNavigationServiceSecondaryNavigationFactory'
    ),
)

看到我在这里做了一个服务 secondary_navigation,然后工厂将返回一个 ZendNavigation 实例.如果你现在执行 $sm->get('secondary_navigation') 你会看到这是一个 ZendNavigationNavigation 对象.

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

第四步

告诉视图助手使用这个导航而不是默认导航.导航视图助手接受一个导航"参数,您可以在其中声明您想要的导航.在这种情况下,服务管理器有一个服务 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天全站免登陆