Zend 3 & Doctrine 无法解析服务 "BookList\Controller\Book" 到工厂;你确定你在配置过程中提供了它吗?
Zend 3 & Doctrine Unable to resolve service "BookList\Controller\Book" to a factory; are you certain you provided it during configuration?
我正在尝试使用 Zend 和 Doctrine 创建一个练习项目。我可以让这个应用程序在没有 Doctrine 或数据库的情况下工作。
但是,最近我尝试使用 Doctrine 添加数据库层并开始收到错误消息:'在插件管理器中找不到名为“getServiceLocator”的插件Zend\Mvc\Controller\PluginManager'。为了解决这个问题,我发现了以下内容: 这几乎完全符合我的情况。该解决方案表明 getServiceLocator() 已被删除,我需要使用 Factory 类 来实例化我的控制器
"Basically, you shouldn't inject dependencies in the middle of
runtime, but actually register a factory for your controller and then
pass dependencies in through a constructor."
这是我的问题: 在实施上面提供的解决方案后,我收到以下错误:'无法将服务 "BookList\Controller\Book" 解析为工厂;您确定您在配置期间提供了它吗?'。我不知道为什么!我究竟做错了什么?为什么我会收到此错误以及为什么上述解决方案对我不起作用?
这是堆栈:
#0 C:\wamp64\www\skeleton-application\vendor\zendframework\zend-servicemanager\src\ServiceManager.php(763): Zend\ServiceManager\ServiceManager->getFactory('BookList\Contro...')
#1 C:\wamp64\www\skeleton-application\vendor\zendframework\zend-servicemanager\src\ServiceManager.php(200): Zend\ServiceManager\ServiceManager->doCreate('BookList\Contro...')
#2 C:\wamp64\www\skeleton-application\vendor\zendframework\zend-servicemanager\src\AbstractPluginManager.php(141): Zend\ServiceManager\ServiceManager->get('BookList\Contro...')
#3 C:\wamp64\www\skeleton-application\vendor\zendframework\zend-mvc\src\DispatchListener.php(102): Zend\ServiceManager\AbstractPluginManager->get('BookList\Contro...')
#4 C:\wamp64\www\skeleton-application\vendor\zendframework\zend-eventmanager\src\EventManager.php(322): Zend\Mvc\DispatchListener->onDispatch(Object(Zend\Mvc\MvcEvent))
#5 C:\wamp64\www\skeleton-application\vendor\zendframework\zend-eventmanager\src\EventManager.php(179): Zend\EventManager\EventManager->triggerListeners(Object(Zend\Mvc\MvcEvent), Object(Closure))
#6 C:\wamp64\www\skeleton-application\vendor\zendframework\zend-mvc\src\Application.php(332): Zend\EventManager\EventManager->triggerEventUntil(Object(Closure), Object(Zend\Mvc\MvcEvent))
#7 C:\wamp64\www\skeleton-application\public\index.php(40): Zend\Mvc\Application->run()
#8 {main}
我的控制器:
<?php
namespace BookList\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use BookList\Form\BookForm;
use Doctrine\ORM\EntityManager; //For DOCTRINE
class BookController extends AbstractActionController{
protected $em; //DOCTRINE Entity Manager
public function __construct(EntityManager $em)
{
$this->em = $em;
}
public function indexAction(){
return new ViewModel(array(
'books' => []; //Do nothing whilst I try to fix factory problem
));
}
}
我的控制器工厂:
<?php
namespace BookList\Controller\Factory;
use Interop\Container\ContainerInterface;
use Zend\ServiceManager\Factory\FactoryInterface;
use BookList\Controller\BookController;
class IndexControllerFactory implements FactoryInterface {
public function __invoke(ContainerInterface $container, $requestedName = "", array $options = null) {
return new BookController($container->get('Doctrine\ORM\EntityManager'));
}
}
?>
module.config.php under module\BookList\config\ - 我注释掉了 'Invokables' 元素和 replaced按照指示(在顶部)使用 'factories' 元素:
<?php
namespace BookList;
return array(
'controllers' => array(
/*
'invokables' => array(
'BookList\Controller\Book' => 'BookList\Controller\BookController',
),
*/
'factories' => array(
'BookList\Controller\Book' => 'BookList\Controller\Factory\BookListControllerFactory',
),
),
'router' => array(
'routes' => array(
'book' => array(
'type' => 'segment',
'options' => array(
'route' => '/book[/][:action][/:id]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'BookList\Controller\Book',
'action' => 'index',
),
),
),
),
),
'view_manager' => array(
'template_path_stack' => array(
'book' => __DIR__ . '/../view',
),
),
// Doctrine config
'doctrine' => array(
'driver' => array(
__NAMESPACE__ . '_driver' => array(
'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
'cache' => 'array',
'paths' => array(__DIR__ . '/../src/' . __NAMESPACE__ . '/Entity')
),
'orm_default' => array(
'drivers' => array(
__NAMESPACE__ . '\Entity' => __NAMESPACE__ . '_driver'
)
)
)
)
);
为了以防万一我的版本控制出现问题,这里是我用来安装 ZF 和 Doctrine 的 Composer.json 文件:
{
"name": "zendframework/skeleton-application",
"description": "Skeleton Application for Zend Framework zend-mvc applications",
"type": "project",
"license": "BSD-3-Clause",
"keywords": [
"framework",
"mvc",
"zf"
],
"homepage": "http://framework.zend.com/",
"minimum-stability": "dev",
"prefer-stable": true,
"require": {
"php": "^5.6 || ^7.0",
"zendframework/zend-component-installer": "^1.0 || ^0.7 || ^1.0.0-dev@dev",
"zendframework/zend-mvc": "^3.1.0",
"zfcampus/zf-development-mode": "^3.0",
"zendframework/zftool": "dev-master",
"doctrine/doctrine-orm-module": "^1.1"
},
"autoload": {
"psr-4": {
"Application\": "module/Application/src/"
}
},
"autoload-dev": {
"psr-4": {
"ApplicationTest\": "module/Application/test/"
}
},
"extra": [],
"scripts": {
"cs-check": "phpcs",
"cs-fix": "phpcbf",
"development-disable": "zf-development-mode disable",
"development-enable": "zf-development-mode enable",
"development-status": "zf-development-mode status",
"post-create-project-cmd": [
"@development-enable"
],
"serve": "php -S 0.0.0.0:8080 -t public public/index.php",
"test": "phpunit"
}
}
您似乎在控制器工厂中给 class 贴上了错误的标签。在 module.config.php 中,您将工厂称为 BookListControllerFactory
,但在控制器工厂中,您将其称为 IndexControllerFactory
。试试这个:
<?php
namespace BookList\Controller\Factory;
use Interop\Container\ContainerInterface;
use Zend\ServiceManager\Factory\FactoryInterface;
use BookList\Controller\BookController;
class BookListControllerFactory implements FactoryInterface {
public function __invoke(ContainerInterface $container, $requestedName = "", array $options = null) {
return new BookController($container->get('Doctrine\ORM\EntityManager'));
}
}
?>
我正在尝试使用 Zend 和 Doctrine 创建一个练习项目。我可以让这个应用程序在没有 Doctrine 或数据库的情况下工作。
但是,最近我尝试使用 Doctrine 添加数据库层并开始收到错误消息:'在插件管理器中找不到名为“getServiceLocator”的插件Zend\Mvc\Controller\PluginManager'。为了解决这个问题,我发现了以下内容:
"Basically, you shouldn't inject dependencies in the middle of runtime, but actually register a factory for your controller and then pass dependencies in through a constructor."
这是我的问题: 在实施上面提供的解决方案后,我收到以下错误:'无法将服务 "BookList\Controller\Book" 解析为工厂;您确定您在配置期间提供了它吗?'。我不知道为什么!我究竟做错了什么?为什么我会收到此错误以及为什么上述解决方案对我不起作用?
这是堆栈:
#0 C:\wamp64\www\skeleton-application\vendor\zendframework\zend-servicemanager\src\ServiceManager.php(763): Zend\ServiceManager\ServiceManager->getFactory('BookList\Contro...')
#1 C:\wamp64\www\skeleton-application\vendor\zendframework\zend-servicemanager\src\ServiceManager.php(200): Zend\ServiceManager\ServiceManager->doCreate('BookList\Contro...')
#2 C:\wamp64\www\skeleton-application\vendor\zendframework\zend-servicemanager\src\AbstractPluginManager.php(141): Zend\ServiceManager\ServiceManager->get('BookList\Contro...')
#3 C:\wamp64\www\skeleton-application\vendor\zendframework\zend-mvc\src\DispatchListener.php(102): Zend\ServiceManager\AbstractPluginManager->get('BookList\Contro...')
#4 C:\wamp64\www\skeleton-application\vendor\zendframework\zend-eventmanager\src\EventManager.php(322): Zend\Mvc\DispatchListener->onDispatch(Object(Zend\Mvc\MvcEvent))
#5 C:\wamp64\www\skeleton-application\vendor\zendframework\zend-eventmanager\src\EventManager.php(179): Zend\EventManager\EventManager->triggerListeners(Object(Zend\Mvc\MvcEvent), Object(Closure))
#6 C:\wamp64\www\skeleton-application\vendor\zendframework\zend-mvc\src\Application.php(332): Zend\EventManager\EventManager->triggerEventUntil(Object(Closure), Object(Zend\Mvc\MvcEvent))
#7 C:\wamp64\www\skeleton-application\public\index.php(40): Zend\Mvc\Application->run()
#8 {main}
我的控制器:
<?php
namespace BookList\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use BookList\Form\BookForm;
use Doctrine\ORM\EntityManager; //For DOCTRINE
class BookController extends AbstractActionController{
protected $em; //DOCTRINE Entity Manager
public function __construct(EntityManager $em)
{
$this->em = $em;
}
public function indexAction(){
return new ViewModel(array(
'books' => []; //Do nothing whilst I try to fix factory problem
));
}
}
我的控制器工厂:
<?php
namespace BookList\Controller\Factory;
use Interop\Container\ContainerInterface;
use Zend\ServiceManager\Factory\FactoryInterface;
use BookList\Controller\BookController;
class IndexControllerFactory implements FactoryInterface {
public function __invoke(ContainerInterface $container, $requestedName = "", array $options = null) {
return new BookController($container->get('Doctrine\ORM\EntityManager'));
}
}
?>
module.config.php under module\BookList\config\ - 我注释掉了 'Invokables' 元素和 replaced按照指示(在顶部)使用 'factories' 元素:
<?php
namespace BookList;
return array(
'controllers' => array(
/*
'invokables' => array(
'BookList\Controller\Book' => 'BookList\Controller\BookController',
),
*/
'factories' => array(
'BookList\Controller\Book' => 'BookList\Controller\Factory\BookListControllerFactory',
),
),
'router' => array(
'routes' => array(
'book' => array(
'type' => 'segment',
'options' => array(
'route' => '/book[/][:action][/:id]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'BookList\Controller\Book',
'action' => 'index',
),
),
),
),
),
'view_manager' => array(
'template_path_stack' => array(
'book' => __DIR__ . '/../view',
),
),
// Doctrine config
'doctrine' => array(
'driver' => array(
__NAMESPACE__ . '_driver' => array(
'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
'cache' => 'array',
'paths' => array(__DIR__ . '/../src/' . __NAMESPACE__ . '/Entity')
),
'orm_default' => array(
'drivers' => array(
__NAMESPACE__ . '\Entity' => __NAMESPACE__ . '_driver'
)
)
)
)
);
为了以防万一我的版本控制出现问题,这里是我用来安装 ZF 和 Doctrine 的 Composer.json 文件:
{
"name": "zendframework/skeleton-application",
"description": "Skeleton Application for Zend Framework zend-mvc applications",
"type": "project",
"license": "BSD-3-Clause",
"keywords": [
"framework",
"mvc",
"zf"
],
"homepage": "http://framework.zend.com/",
"minimum-stability": "dev",
"prefer-stable": true,
"require": {
"php": "^5.6 || ^7.0",
"zendframework/zend-component-installer": "^1.0 || ^0.7 || ^1.0.0-dev@dev",
"zendframework/zend-mvc": "^3.1.0",
"zfcampus/zf-development-mode": "^3.0",
"zendframework/zftool": "dev-master",
"doctrine/doctrine-orm-module": "^1.1"
},
"autoload": {
"psr-4": {
"Application\": "module/Application/src/"
}
},
"autoload-dev": {
"psr-4": {
"ApplicationTest\": "module/Application/test/"
}
},
"extra": [],
"scripts": {
"cs-check": "phpcs",
"cs-fix": "phpcbf",
"development-disable": "zf-development-mode disable",
"development-enable": "zf-development-mode enable",
"development-status": "zf-development-mode status",
"post-create-project-cmd": [
"@development-enable"
],
"serve": "php -S 0.0.0.0:8080 -t public public/index.php",
"test": "phpunit"
}
}
您似乎在控制器工厂中给 class 贴上了错误的标签。在 module.config.php 中,您将工厂称为 BookListControllerFactory
,但在控制器工厂中,您将其称为 IndexControllerFactory
。试试这个:
<?php
namespace BookList\Controller\Factory;
use Interop\Container\ContainerInterface;
use Zend\ServiceManager\Factory\FactoryInterface;
use BookList\Controller\BookController;
class BookListControllerFactory implements FactoryInterface {
public function __invoke(ContainerInterface $container, $requestedName = "", array $options = null) {
return new BookController($container->get('Doctrine\ORM\EntityManager'));
}
}
?>