Starting with version 2.7.0, zend-mvc ServiceLocatorAwareInterface loses, so it $this->serviceLocator->get()calls inside the controllers.
That's why a few days ago I did a huge refactoring of all my modules to implement the necessary services / objects through constructors using factories for most of everything.
Of course, I understand why this is a better / cleaner way to do something, because the dependencies are now much more noticeable. But on the other side:
This leads to heavy overhead and much more unused class instances, right?
Let's look at an example:
Since all of my controllers are addicted, I created factories for everyone.
<i> CustomerControllerFactory.php
namespace Admin\Factory\Controller;
class CustomerControllerFactory implements FactoryInterface {
public function createService(ServiceLocatorInterface $controllerManager) {
$serviceLocator = $controllerManager->getServiceLocator();
$customerService = $serviceLocator->get('Admin\Service\CustomerService');
$restSyncService = $serviceLocator->get('Admin\Service\SyncRestClientService');
return new \Admin\Controller\CustomerController($customerService, $restSyncService);
}
}
< > CustomerController.php
namespace Admin\Controller;
class CustomerController extends AbstractRestfulController {
public function __construct($customerService, $restSyncService) {
$this->customerService = $customerService;
$this->restSyncService = $restSyncService;
}
}
< > module.config.php
'controllers' => [
'factories' => [
'Admin\Controller\CustomerController' => 'Admin\Factory\Controller\CustomerControllerFactory',
]
],
'service_manager' => [
'factories' => [
'Admin\Service\SyncRestClientService' => 'Admin\Factory\SyncRestClientServiceFactory',
]
]
< > SyncRestClientServiceFactory.php
namespace Admin\Factory;
class SyncRestClientServiceFactory implements FactoryInterface {
public function createService(ServiceLocatorInterface $serviceLocator) {
$entityManager = $serviceLocator->get('doctrine.entitymanager.orm_default');
$x1 = $serviceLocator->get(...);
$x2 = $serviceLocator->get(...);
$x3 = $serviceLocator->get(...);
return new \Admin\Service\SyncRestClientService($entityManager, $x1, $x2, $x3, ...);
}
}
SyncRestService - , . , CustomerController. sync-service syncAction() CustomerController! $this->serviceLocator->get('Admin\Service\SyncRestClientService') syncAction(), .
, , , . - " "?