我开始学习 ZF2,但 ServiceManager 有问题。
我试图将新模块“菜单”添加到 ServiceManager。
在我的 application/config/module.config.php 中添加了以下代码:
'service_manager' => array(
'factories' => array(
'translator' => 'Zend\I18n\Translator\TranslatorServiceFactory',
'menu' => 'Application\Menu\MenuServiceFactory',
),
),
'menu' => array(
),
应用程序\src\Menu\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\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}
有没有人有解决这个错误的想法?