I am trying to create a practice project using Zend and Doctrine. I can get this application to work without Doctrine or a database.
However, recently I tried adding a database layer using Doctrine and started getting the error: 'A plugin by the name “getServiceLocator” was not found in the plugin manager Zend\Mvc\Controller\PluginManager'. To fix this I found the following: A plugin by the name "getServiceLocator" was not found in the plugin manager Zend\Mvc\Controller\PluginManager This matches my situation almost exactly. The solution indicates that getServiceLocator() is depricated and I need to be using Factory classes to instantiate my controllers
"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."
Here is my problem: after implementing the solution provided above I get the following error: 'Unable to resolve service "BookList\Controller\Book" to a factory; are you certain you provided it during configuration?'. And I can't work out how why! What am I doing wrong? Why do I get this error and why does the above solution not work for me?
Here is the stack:
#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}
My Controller:
<?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
));
}
}
My Controller Factory:
<?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\ - I commented out the 'Invokables' element and replaced it with the 'factories' element as instructed (at the top):
<?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'
)
)
)
)
);
And just in case it's a problem with my versioning here is the Composer.json file that I used to install both ZF and Doctrine:
{
"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"
}
}