I'm trying to inject the service manager into a controller.
Actual Error:
\vendor\zendframework\zend-servicemanager\src\Exception\ServiceLocatorUsageException.php:34
Service "Project\Service\ProjectServiceInterface" has been requested to plugin manager of type "Zend\Mvc\Controller\ControllerManager", but couldn't be retrieved. A previous exception of type "Zend\ServiceManager\Exception\ServiceNotFoundException" has been raised in the process. By the way, a service with the name "Project\Service\ProjectServiceInterface" has been found in the parent service locator "Zend\ServiceManager\ServiceManager": did you forget to use $parentLocator = $serviceLocator->getServiceLocator() in your factory code?
The process goes:
class BaseController extends AbstractActionController implements ServiceLocatorAwareInterface { public function __construct(\Zend\ServiceManager\ServiceLocatorInterface $sl) { $this->serviceLocator = $sl; } } - Create controller and use constructor method
- Extend this
BaseControllertoAdminController - Setup Routes to
AdminController => /admin use
Module.phppublic function getControllerConfig()Use closer as factory to create controller object injecting the serviceLocator
'Project\Controller\Project' => function($sm) { $serviceLocator = $sm->getServiceLocator(); return new \Project\Controller\ProjectController($serviceLocator); },
- try to use
$this->getServiceLocator()->get('service_name') - Exception found for missing service.....
Now the problem is this:
/** * * @param ServiceLocatorInterface $sl */ public function __construct(\Zend\ServiceManager\ServiceLocatorInterface $sl) { $rtn = $sl->has('Project\Service\ProjectServiceInterface'); echo '<br />in Constructor: '.__FILE__;var_dump($rtn); $this->serviceLocator = $sl; } public function getServiceLocator() { $rtn = $this->serviceLocator->has('Project\Service\ProjectServiceInterface'); echo '<br />in getServiceLocator: '.__FILE__;var_dump($rtn); return $this->serviceLocator; } Within the __constructor() the service IS FOUND. Within the getServiceLocator() method the service with the same name IS NOT FOUND....
in Constructor: Project\Controller\BaseController.php bool(true) in getServiceLocator: Project\Controller\BaseController.php bool(false)
Am I missing something? Is the SharedServiceManager doing something here?
The entire purpose of this exercise was due to this message:
Deprecated: ServiceLocatorAwareInterface is deprecated and will be removed in version 3.0, along with the ServiceLocatorAwareInitializer. ...
controllerandservice_managerconfiguration?