zf2 navigation - 'Zend \ ServiceManager \ ServiceManager :: get无法获取或创建导航实例'

时间:2012-12-14 13:48:39

标签: zend-framework2

Hy大家!

我正在学习zf2,并尝试设置导航面板(基于:Zend Framework 2: Zend_Navigation),但计算机的答案仍然是:

发生错误 执行期间发生错误;请稍后再试。 附加信息: Zend的\的ServiceManager \异常\ ServiceNotFoundException的 文件: /var/www/zf2-tutorial/vendor/zendframework/zendframework/library/Zend/ServiceManager/ServiceManager.php:453 信息: Zend \ ServiceManager \ ServiceManager :: get无法获取或创建导航实例

module.config.php包含:

   '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,它看起来像:

<?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',

               )
        ),
    ),

);

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

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

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

感谢您的帮助!

3 个答案:

答案 0 :(得分:3)

如果你看一下Naviagtion类的构造函数,它需要一个Pages数组或一个Traversable对象。你传入一个包含内部数组的数组,试试这个:

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

            return $navigation;
        }
    ),
),

默认出厂设置将通过查找导航配置下的默认节点自动找到您的配置。

您也可以按照自己的方式保留它,但是将“默认”节点(在配置中的导航节点下)传递给Navigation构造函数,因为这是实际的页面定义。

答案 1 :(得分:0)

当我更改application.config.php

中的以下行时,我的问题已解决
'config/autoload/{,*.}{global,local}.php', //[This is default setting]

config_glob_paths' => array( '<PUT_ABSOLUTE_PATH_HERE>{,*.}{global,local}.php', ),

代替'config/autoload'完整路径而不是相对路径。

答案 2 :(得分:0)

我认为您正在使用视图助手

$这 - &GT;逃($这 - &GT; somevar); //这是zf1 view helper

在zf2中,您可以使用视图助手

进行转义

$这 - &GT; escapeHtml($这 - &GT; somevar);