ServiceNotCreatedException in Zend Framework 2 while attempting multiple navigations

半腔热情 提交于 2020-01-05 05:59:34

问题


I am attempting to create multiple navigation menus to use in my application based on a specific user role. The majority of the code is similar to zfc-admin. When I use zfc-admin in my application I am able to bring up an admin menu, however I will have about four roles, and decided to put this in my Application module.

module.config.php

'navigation' => array(
    'admin' => array(
        array(
            'label' => 'Admin Home',
            'route' => 'adminhome',
        ),
    ),
    'default' => array(
        array(
            'label' => 'Home',
            'route' => 'home',
        ),
    ),
),

AdminNavigationFactory.php

namespace Application\Navigation\Service;
use Zend\Navigation\Service\DefaultNavigationFactory;

class AdminNavigationFactory extends DefaultNavigationFactory
{
    protected function getName()
    {
        return 'admin';
    }
}

Module.php

public function getServiceConfig()
{
    return array(
        'factories' => array(
            'admin_navigation' => 'Application\Navigation\Service\AdminNavigationFactory',
        ),
    );
}

layout.phtml

<?php echo $this->navigation('admin_navigation')->menu(); ?>

I get the error.

Fatal error: Uncaught exception 'Zend\ServiceManager\Exception\ServiceNotCreatedException' with message 'While attempting to create adminnavigation(alias: admin_navigation) an invalid factory was registered for this instance type.' in /Applications/MAMP/htdocs/myapp/vendor/zendframework/zendframework/library/Zend/ServiceManager/ServiceManager.php on line 987

If I change the layout.phtml to use the default menu everything works as expected.

<?php echo $this->navigation('navigation')->menu(); ?>

回答1:


first I got exactly the same issue. After moving the factory configuration from the getServiceConfig() method in the module class to the module.config.php it worked.

So my admin navigation works now like the follows:

module.config.php

(module/Admin/config/module.config.php)

return array(
    // yada yada yada...
    'service_manager' => array(
        'factories' => array(
            'navigation' => 'Zend\Navigation\Service\DefaultNavigationFactory',
            'adminnav' => 'Application\Navigation\Service\AdminNavigationFactory',
        ),
    ),
    'navigation'      => array(
        'default' => array(
            array(
                'label' => 'Home',
                'route' => 'home',
            ),
            array(
                'label' => 'Filme',
                'route' => 'movies',
            ),
            array(
                'label' => 'Admin',
                'route' => 'admin',
            ),
        ),
        'adminnav' => array(
            array(
                'label' => 'Film hinzufügen',
                'route' => 'add-movie',
            ),
            array(
                'label' => 'Buch hinzufügen',
                'route' => 'add-book',
            ),
        ),
    ),
);

AdminNavigationFactory

(module/Application/src/Application/Navigation/Service/AdminNavigationFactory.php)

namespace Application\Navigation\Service;

use Zend\Navigation\Service\DefaultNavigationFactory;

class AdminNavigationFactory extends DefaultNavigationFactory
{
    protected function getName()
    {
        return 'adminnav';
    }
} 

Maybe you want to check out the code in context of the entire application, so here are the links to the my github:

  1. AdminNavigationFactory
  2. module.config.php

Regards, Sascha



来源:https://stackoverflow.com/questions/18003514/servicenotcreatedexception-in-zend-framework-2-while-attempting-multiple-navigat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!