我正在尝试使用Zend和Doctrine创建一个练习项目。我可以让这个应用程序在没有Doctrine或数据库的情况下工作。
但是,最近我尝试使用Doctrine添加数据库层并开始收到错误:' 在插件管理器Zend \ Mvc \ Controller \ PluginManager中找不到名为“getServiceLocator”的插件/ EM>&#39 ;.为了解决这个问题,我发现了以下内容:A plugin by the name "getServiceLocator" was not found in the plugin manager Zend\Mvc\Controller\PluginManager这几乎完全符合我的情况。解决方案表明getServiceLocator()已被删除,我需要使用Factory类来实例化我的控制器
"基本上,你不应该在中间注入依赖关系 运行时,但实际上为您的控制器注册工厂然后 通过构造函数传递依赖关系。"
这是我的问题:在实施上面提供的解决方案后,我收到以下错误:' 无法解析服务" 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'));
}
}
?>
模块\ BookList \ config \下的module.config.php - 我用注释了&#39; Invokables&#39; 元素,替换了 >&#39;工厂&#39; 元素按照指示(在顶部):
<?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"
}
}
答案 0 :(得分:1)
看起来您在控制器工厂中错误地标记了该类。在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'));
}
}
?>