ZF2 How to get access to the service manager on a custom class that is no a controller, helper or service

4.4k views Asked by At

guys, At this point i am close to start pulling hair out of my head. I don't find a way to achieve this.

I have a custom class that belongs to a custom folder i created under my WebServices Module src folder. I need to be able to instantiate this class from inside another module/controller but when i do that and dump the services member it contains null. How can i have the service manager accesible from inside my ApiAuthentication class.

Any help will be appreciated. Thanks

<?php

namespace WebServices\Services;

use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

class ApiAuthenticationService extends \Zend\Soap\Client implements ServiceLocatorAwareInterface{

    public $services;

    function __construct($options = null){

        parent::__construct('http://tinysoa.local/soap/security/api_authentication?wsdl',$options);

    }

    public function setServiceLocator(ServiceLocatorInterface $locator)
    {
        $this->services = $locator;
    }

    public function getServiceLocator()
    {
        return $this->services;
    }

}

When i call this from inside another module/controller it dumps a null value:

class IndexController extends AbstractActionController
{

       public function indexAction()
            {
                $a = new \WebServices\Services\ApiAuthenticationService();

                var_dump($a->services);
2

There are 2 answers

0
timdev On BEST ANSWER

Responding with my own answer to add-on to Adrian's, and the question you asked in response.

If your service has dependencies of it's own, you just use a factory instead of going the invokable route.

Say your service needs a cache adapter and database adapter. Also imagine that it can optionally be configured with some other service (FooService, below):

<?php
public function getServiceConfig()
{
    return array(
        'factories' => array(
            'my_service' => function($sm){
                $cache = $sm->get('Cache');
                $dbAdapter = $sm->get('DefaultDbAdapter');
                $fooService = $sm->get('FooService');

                // instantiate your service with required dependencies
                $mySvc = new \My\Shiny\Service($cache, $dbAdapter);

                // inject an optional dependency
                $mySvc->setFooService($fooService);

                // return your shiny new service
                return $mySvc;
            }
        )
    );
}

Side Note: It's generally bad design to inject the ServiceManager all over the place. You're better off managing your dependencies more explicitly, like above.

This stuff is covered quite well in the Quick Start, if you haven't already read that.

2
Adrian Nowicki On

Register your Service in Service Config and access it through getServiceLocator() method in controller.

Module.php

public function getServiceConfig()
{
  return array(
    'invokables' => array(
        'my_service' => 'WebServices\Services\ApiAuthenticationService'
    )
  );
}

Controller

public function indexAction()
{
    $service = $this->getServiceLocator()->get('my_service');
}