Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are two ways of doing this:</p> <ol> <li>Add the model as a service in the <code>ServiceManager</code> configuration, and make sure the model class implements the <code>Zend\Service\ServiceLocatorAwareInterface</code> class.</li> <li>Manually add the service manager into the model by a getter/setter through another class that uses the <code>ServiceManager</code>, eg. a <code>Controller</code>.</li> </ol> <p>Method 1:</p> <pre><code>// module.config.php &lt;?php return array( 'service_manager' =&gt; array( 'invokables' =&gt; array( 'ProjectGateway' =&gt; 'Application\Model\ProjectGateway', ) ) ); </code></pre> <p>Now make sure your model implements the <code>ServiceLocatorAwareInterface</code> and its methods:</p> <pre><code>namespace Application\Model; use Zend\ServiceManager\ServiceLocatorAwareInterface; use Zend\ServiceManager\ServiceLocatorInterface; class ProjectGateway implements ServiceLocatorAwareInterface { protected $serviceLocator; public function setServiceLocator(ServiceLocatorInterface $serviceLocator) { $this-&gt;serviceLocator = $serviceLocator; } public function getServiceLocator() { return $this-&gt;serviceLocator; } } </code></pre> <p>From a controller you are now able to get your <code>ProjectGateway</code> by doing:</p> <pre><code>$projectGateway = $this-&gt;getServiceLocator-&gt;get('ProjectGateway'); </code></pre> <p>Consequently you have now the ServiceManager available in your <code>ProjectGateway</code> class by doing the following:</p> <pre><code>public function someMethodInProjectGateway() { $serviceManager = $this-&gt;getServiceLocator(); } </code></pre> <p><strong>UPDATE 04/06/2014:</strong> Method 2:</p> <p>Basically what you need in your model is a getter/setter for the <code>ServiceManager</code> as shown in method 1, which is shown below:</p> <pre><code>namespace Application\Model; use Zend\ServiceManager\ServiceLocatorAwareInterface; use Zend\ServiceManager\ServiceLocatorInterface; class ProjectGateway implements ServiceLocatorAwareInterface { protected $serviceLocator; public function setServiceLocator(ServiceLocatorInterface $serviceLocator) { $this-&gt;serviceLocator = $serviceLocator; } public function getServiceLocator() { return $this-&gt;serviceLocator; } } </code></pre> <p>Then all you need to do from somewhere else (e.g. a <code>Controller</code>) is parse the <code>ServiceManager</code> in there:</p> <pre><code>namespace Application\Controller; use Zend\Mvc\Controller\AbstractActionController; use Application\Model\ProjectGateway; class SomeController extends AbstractActionController { public function someAction() { $model = new ProjectGateway(); // Now set the ServiceLocator in our model $model-&gt;setServiceLocator($this-&gt;getServiceLocator()); } } </code></pre> <p>It is nothing more than that. </p> <p>Using method 2 however means that the <code>ProjectGateway</code> model is not readily available throughout your application on-demand. You'll need to instantiate and set the <code>ServiceManager</code> every time. </p> <p>However, as a last note, it must be noted that method 1 is not that much heavier in resources than method 2, as the model is not instantiated before you call it for the first time.</p>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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