zf2 导航 - “Zend\ServiceManager\ServiceManager::get 无法获取或创建导航实例" [英] zf2 navigation - 'Zend\ServiceManager\ServiceManager::get was unable to fetch or create an instance for navigation'

查看:25
本文介绍了zf2 导航 - “Zend\ServiceManager\ServiceManager::get 无法获取或创建导航实例"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!

我正在学习 zf2,并尝试设置导航面板(基于:Zend Framework2: Zend_Navigation),但是电脑的回答还是:

I am learning zf2, and trying to set up a navigation panel(based on: Zend Framework 2: Zend_Navigation), but the answer from the computer is still:

发生错误执行过程中出现错误;请稍后再试.附加信息:Zend\ServiceManager\Exception\ServiceNotFoundException文件:/var/www/zf2-tutorial/vendor/zendframework/zendframework/library/Zend/ServiceManager/ServiceManager.php:453信息:Zend\ServiceManager\ServiceManager::get 无法获取或创建导航实例

An error occurred An error occurred during execution; please try again later. Additional information: Zend\ServiceManager\Exception\ServiceNotFoundException File: /var/www/zf2-tutorial/vendor/zendframework/zendframework/library/Zend/ServiceManager/ServiceManager.php:453 Message: Zend\ServiceManager\ServiceManager::get was unable to fetch or create an instance for navigation

module.config.php 包含:

The module.config.php contain:

   'servicemanager' => array(
        'factories' => array(
            'navigation' => function($sm) {
                $config = $sm->get('Config');
                $navigation = new \Zend\Navigation\Navigation($config->get('navigation'));
                return $navigation;
            }
        ),
    ),

我在主 config/autoload 文件夹中有一个 application.global.php,如下所示:

I have an application.global.php in the main config/autoload folder which is looks like:

<?php

return array(
    // All navigation-related configuration is collected in the 'navigation' key
    'navigation' => array(
        // The DefaultNavigationFactory we configured in (1) uses 'default' as the sitemap key
        'default' => array(
            // And finally, here is where we define our page hierarchy
            'Album' => array(            
                    'label'      => 'Albumlista',
                    'route'      => 'album',
                    'action' => 'index',
                    'pages'      => array(
                        array(
                            'label'      => 'Add',
                            'route'      => 'album',
                            'action'     => 'add'
                            )           
                       )
               ),
            'Application' => array(            
                    'label'      => 'Alap alkalmazás',
                    'route'      => 'application',
                    'action' => 'index',

               )
        ),
    ),

);

我从控制器发出这个命令:

And from the controller i give this command:

$config = $this->getServiceLocator()->get('navigation');

有人能帮我解决这个问题吗?我读到了 http://adam.lundrigan.ca/2012/07/quick-and-dirty-zf2-zend-navigation/ ,我试过了,我做到了,但我想结合acl,所以我写了这个问题.

Could somebody help me to solve this problem? I read about http://adam.lundrigan.ca/2012/07/quick-and-dirty-zf2-zend-navigation/ , I tried it, and I did it, but I would like to combine with acl so that I wrote this question.

感谢大家的帮助!

推荐答案

如果您查看 Naviagtion 类的构造函数,它需要一个 Pages 数组或一个 Traversable 对象.您正在传入一个包含页面数组的数组,试试这个:

If you look at the contructor for the Naviagtion class it required either an array of Pages or a Traversable object. You are passing in an array with an array of pages inside, try this:

'servicemanager' => array(
    'factories' => array(
        'navigation' => function($sm) {
            $navigation = new \Zend\Navigation\Service\DefaultNavigationFactory;
            $navigation = $navigation->createService($sm);

            return $navigation;
        }
    ),
),

默认工厂会通过在您的导航配置下查找默认节点来自动找到您的配置.

The default factory will automatically find your config by looking for the default node under your navigation config.

您也可以保持原样,但将默认"节点(在配置中的导航节点下)传递给导航构造函数,因为这是实际的页面定义.

You could alternatively keep it the way you have, but pass in the "default" node (under the Navigation node in the config) to the Navigation contructor as this is the actual page definition.

这篇关于zf2 导航 - “Zend\ServiceManager\ServiceManager::get 无法获取或创建导航实例"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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