Zend Framework 2中的ServiceNotCreatedException尝试多次导航时 [英] ServiceNotCreatedException in Zend Framework 2 while attempting multiple navigations
问题描述
我正在尝试基于特定用户角色创建多个导航菜单以在我的应用程序中使用.大多数代码类似于zfc-admin.当我在应用程序中使用zfc-admin时,我可以调出一个管理菜单,但是我将有大约四个角色,并决定将其放在我的应用程序"模块中.
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(); ?>
我得到了错误.
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
如果我将layout.phtml更改为使用默认菜单,那么一切都会按预期进行.
If I change the layout.phtml to use the default menu everything works as expected.
<?php echo $this->navigation('navigation')->menu(); ?>
推荐答案
首先,我遇到了完全相同的问题.将工厂配置从模块类中的getServiceConfig()方法移动到module.config.php后,它可以正常工作.
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/Admin/config/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
(模块/应用程序/src/应用程序/导航/服务/AdminNavigationFactory.php)
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';
}
}
也许您想在整个应用程序的上下文中签出代码,所以这里是我的github的链接:
Maybe you want to check out the code in context of the entire application, so here are the links to the my github:
- AdminNavigationFactory
- module.config.php
关于, 萨沙(Sascha)
Regards, Sascha
这篇关于Zend Framework 2中的ServiceNotCreatedException尝试多次导航时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!