Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to inject ServiceManager into a user defined class
    primarykey
    data
    text
    <p>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."</p> <p>so I tried this:</p> <pre><code>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-&gt;init(); } /** * Extend Constructor */ public function init() {} /** * (non-PHPdoc) * * @see \Zend\ServiceManager\ServiceLocatorAwareInterface::setServiceLocator() * */ public function setServiceLocator(ServiceLocatorInterface $serviceLocator) { $this-&gt;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-&gt;getServiceLocator()-&gt;get('Log\Web'); throw new ConnectionException(); } } /** * (non-PHPdoc) * * @see \Zend\ServiceManager\ServiceLocatorAwareInterface::getServiceLocator() * */ public function getServiceLocator() { return $this-&gt;serviceLocator; } } </code></pre> <p>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.</p> <p>Seem that the service manager is not being injected to my abstract class, and the return of getServiceLocator is NULL.</p> <p>Any thing I missed to make it right?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload