0
votes

In the doc it's said:"By default, the Zend Framework MVC registers an initializer that will inject the ServiceManager instance, which is an implementation of Zend\ServiceManager\ServiceLocatorInterface, into any class implementing Zend\ServiceManager\ServiceLocatorAwareInterface."

so I tried this:

interface ModelResourceInterface extends ServiceLocatorAwareInterface
{
}

interface ServiceModelResourceInterface extends ModelResourceInterface
{
    public function fetch($uri, $method, $parameters, $options, $encodeType);
}


namespace Ssports\Model\Resource\Service\Http;

use Ssports\Model\Resource\Service\ServiceModelResourceInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\Http\Client;
use Zend\Http\Request;
use Ssports\Model\Resource\Service\ConnectionException;

abstract class AbstractHttpServiceModelResource implements ServiceModelResourceInterface
{

    /**
     *
     * @var Zend\ServiceManager\ServiceLocatorInterface;
     */
    protected $serviceLocator;

    /**
     * Constructor
     */
    function __construct()
    {
        $this->init();
    }

    /**
     * Extend Constructor
     */
    public function init()
    {}

    /**
     * (non-PHPdoc)
     *
     * @see \Zend\ServiceManager\ServiceLocatorAwareInterface::setServiceLocator()
     *
     */
    public function setServiceLocator(ServiceLocatorInterface $serviceLocator)
    {
        $this->serviceLocator = $serviceLocator;
    }

    /**
     * (non-PHPdoc)
     *
     * @see \Ssports\Model\Resource\Service\ServiceModelResourceInterface::fetch()
     *
     */
    public function fetch($uri, $method, $parameters = null, $options = null, $encodeType = null)
    {
        try {
                //something raise \RuntimeException
        } catch (\RuntimeException $e) {
            $this->getServiceLocator()->get('Log\Web');
            throw new ConnectionException();
        }
    }

    /**
     * (non-PHPdoc)
     *
     * @see \Zend\ServiceManager\ServiceLocatorAwareInterface::getServiceLocator()
     *
     */
    public function getServiceLocator()
    {
        return $this->serviceLocator;
    }
}

I extend this abstract class with some model resource class, and run it, and an exception throw to say that I'm calling get on a non-object.

Seem that the service manager is not being injected to my abstract class, and the return of getServiceLocator is NULL.

Any thing I missed to make it right?

1
It's better to only inject the classes that you need instead of the full registry of classes. Right now all i see is a "logger" - inject that logger, not the ServiceManager!Sam

1 Answers

0
votes

Have you tried to use the service locator trait?

It can be found in \Zend\ServiceManager\ServiceLocatorAwareTrait

However this requires PHP 5.4 to work...

To use a trait do the following

class Class1
{
  use Zend\ServiceManager\ServiceLocatorAwareTrait;
}

You can access the service locator then, or at least that is how i had to do it when i needed to load the service locator.