我开始学习ZF2但是ServiceManager有问题。
我尝试将新模块“Menu”添加到ServiceManager。
在我的application / config / module.config.php中添加了以下代码:
'service_manager' => array(
'factories' => array(
'translator' => 'Zend\I18n\Translator\TranslatorServiceFactory',
'menu' => 'Application\Menu\MenuServiceFactory',
),
),
'menu' => array(
),
应用程序\ SRC \菜单\ MenuServiceFactory.php:
namespace Application\Menu;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
/**
* Menu
*/
class MenuServiceFactory implements FactoryInterface
{
public function createService(ServiceLocatorInterface $serviceLocator)
{
// Configure the Menu
$config = $serviceLocator->get('Config');
$menuConfig = isset($config['menu']) ? $config['menu'] : array();
$menu = Menu::factory($menuConfig);
return $menu;
}
}
应用程序\ SRC \菜单\ Menu.php:
namespace Application\Menu;
/**
* Menu
*/
class Menu
{
/**
* Instantiate a menu
*
* @param array $options
* @return Menu
*/
public static function factory($options)
{
$menu = 'test';
return $menu;
}
}
如果我尝试添加此“$ menu = $ this-> getServiceLocator() - > get('menu');”我收到了:
Zend\ServiceManager\Exception\ServiceNotCreatedException
File:
E:\Projects\zf2\vendor\zendframework\zendframework\library\Zend\ServiceManager\ServiceManager.php:871
Message:
While attempting to create menu(alias: menu) an invalid factory was registered for this instance type.
Stack trace:
#0 E:\Projects\zf2\vendor\zendframework\zendframework\library\Zend\ServiceManager\ServiceManager.php(494): Zend\ServiceManager\ServiceManager->createFromFactory('menu', 'menu')
#1 E:\Projects\zf2\vendor\zendframework\zendframework\library\Zend\ServiceManager\ServiceManager.php(441): Zend\ServiceManager\ServiceManager->create(Array)
#2 E:\Projects\zf2\module\AdminSettings\src\AdminSettings\Controller\SettingsController.php(193): Zend\ServiceManager\ServiceManager->get('menu')
#3 E:\Projects\zf2\vendor\zendframework\zendframework\library\Zend\Mvc\Controller\AbstractActionController.php(83): AdminSettings\Controller\SettingsController->logoAction()
#4 [internal function]: Zend\Mvc\Controller\AbstractActionController->onDispatch(Object(Zend\Mvc\MvcEvent))
#5 E:\Projects\zf2\vendor\zendframework\zendframework\library\Zend\EventManager\EventManager.php(472): call_user_func(Array, Object(Zend\Mvc\MvcEvent))
#6 E:\Projects\zf2\vendor\zendframework\zendframework\library\Zend\EventManager\EventManager.php(207): Zend\EventManager\EventManager->triggerListeners('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#7 E:\Projects\zf2\vendor\zendframework\zendframework\library\Zend\Mvc\Controller\AbstractController.php(117): Zend\EventManager\EventManager->trigger('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#8 E:\Projects\zf2\vendor\zendframework\zendframework\library\Zend\Mvc\DispatchListener.php(114): Zend\Mvc\Controller\AbstractController->dispatch(Object(Zend\Http\PhpEnvironment\Request), Object(Zend\Http\PhpEnvironment\Response))
#9 [internal function]: Zend\Mvc\DispatchListener->onDispatch(Object(Zend\Mvc\MvcEvent))
#10 E:\Projects\zf2\vendor\zendframework\zendframework\library\Zend\EventManager\EventManager.php(472): call_user_func(Array, Object(Zend\Mvc\MvcEvent))
#11 E:\Projects\zf2\vendor\zendframework\zendframework\library\Zend\EventManager\EventManager.php(207): Zend\EventManager\EventManager->triggerListeners('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#12 E:\Projects\zf2\vendor\zendframework\zendframework\library\Zend\Mvc\Application.php(294): Zend\EventManager\EventManager->trigger('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#13 E:\Projects\zf2\public\index.php(13): Zend\Mvc\Application->run()
#14 {main}
有没有人有想法解决此错误?
答案 0 :(得分:12)
MenuServiceFactory.php
文件的路径与您在服务管理器配置中定义的命名空间不匹配。
您需要将Application/src/Menu
文件夹及其包含的所有文件移至Application/src/Application/Menu