问题
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:
- AdminNavigationFactory
- module.config.php
Regards, Sascha
来源:https://stackoverflow.com/questions/18003514/servicenotcreatedexception-in-zend-framework-2-while-attempting-multiple-navigat