title: Zend Framework 2: Pass Variable to View Helper
body: <p>I have created a View Helper to display latest Adverts from a Database Table. Since I have different Types of Adverts, I would like to be able to pass a variable from inside my View where I call the View Helper to show specific Adverts.</p>
  
  <p>I am sorry that I can not explain it in a better way, but I am still a total beginner in ZF2. I will add the Sourcecode and hopefully this will make it more clear. Please note that I have the Sourcecode from a Book which displayed Pizza's randomly and changed it till it worked to show my adverts. I might still have Code in it which is not actually needed, so please do not wonder... Okay here the code: </p>
  
  <p><strong>1. the view: index.html</strong></p>
  
  <pre><code>&lt;?php foreach ($this-&gt;latestAdvert() as $value){ ?&gt;
        &lt;li&gt;&lt;?php echo $value-&gt;getAdvertTitle();?&gt;&lt;/li&gt;
  &lt;?php }?&gt;
  </code></pre>
  
  <p><strong>2. the view Helper: Advert\View\Helper\LatestAdvert.php</strong></p>
  
  <pre><code>namespace Advert\View\Helper;
  use Zend\View\Helper\AbstractHelper;
  
  class LatestAdvert extends AbstractHelper
  {
  
     protected $random = null;
  
  
     public function __construct($random)
     {
       $this-&gt;setLatestAdvert($random);
     }
  
  
     public function setLatestAdvert($random)
     {
       $this-&gt;random = $random;
     }
  
  
     public function getLatestAdvert()
     {
       return $this-&gt;random;
     }
  
  
     public function __invoke()
     {
       $latestAdverts = $this-&gt;getLatestAdvert();
       return $latestAdverts;
     }
  }
  </code></pre>
  
  <p><strong>3. the Factory: Advert\View\Helper\LatestAdvertFactory.php</strong></p>
  
  <pre><code>namespace Advert\View\Helper;
  
  use Zend\ServiceManager\FactoryInterface;
  use Zend\ServiceManager\ServiceLocatorInterface;
  
  class LatestAdvertFactory implements FactoryInterface
  {
  
      public function createService(ServiceLocatorInterface $serviceLocator) 
      {
        $locator = $serviceLocator-&gt;getServiceLocator();
        $service = $locator-&gt;get('Advert\Service');
        $random  = $service-&gt;fetchSingleByRandom();
        $helper  = new LatestAdvert($random);
  
        return $helper;
      }
  }
  </code></pre>
  
  <p><strong>4. the Service: Advert\Service\LatestAdvertService .php</strong></p>
  
  <pre><code>namespace Advert\Service;
  
  use Advert\Entity\Advert as AdvertEntity;
  use Doctrine\ORM\EntityManager;
  use Zend\ServiceManager\ServiceManager;
  use Zend\ServiceManager\ServiceManagerAwareInterface;
  use Zend\Debug\Debug;
  
  class LatestAdvertService implements ServiceManagerAwareInterface
  {
  
    /**
     * Service manager.
     * @var Zend\ServiceManager\ServiceManager
     */
     private $serviceManager = null;
  
    /**
     * Sets service manager.
     * @param Zend\ServiceManager\ServiceManager $serviceManager Service manager.
     */
    public function setServiceManager(ServiceManager $serviceManager)
    {
      $this-&gt;serviceManager = $serviceManager;
    }
  
    /**
     * Returns service manager.
     * @return type
     */
    public function getServiceLocator()
    {
      return $this-&gt;serviceManager;
    }
  
  
    public function fetchSingleByRandom()
    {     
      // Get Doctrine entity manager.
      $entityManager =  $this-&gt;getServiceLocator()
                             -&gt;get('doctrine.entitymanager.orm_default');
      $advertType = 'wanted'; // This should be removed
      $random = $entityManager-&gt;getRepository('Advert\Entity\Advert')
                              -&gt;findAdvertsByDate($advertType);
  
      return $random; 
    }
  }
  </code></pre>
  
  <p><strong>5. Module: Advert\Module.php</strong></p>
  
  <pre><code>public function getServiceConfig()
  {
      return array(
          'invokables' =&gt; array(
                   'Advert\Service' =&gt; 'Advert\Service\LatestAdvertService',
                      ),
      );
  }
  
  public function getViewHelperConfig()
  {
      return array(
          'factories' =&gt; array(           
              'latestAdvert' =&gt; 'Advert\View\Helper\LatestAdvertFactory',
          ),
  
      );
  }
  </code></pre>
  
  <p>As you can see in #4 I have a Variable called $advertType. I want to set the variable when I call the view Helper in my index.html, f.e. $this->latestAdvert('wanted'), but how can I pass this variable through all my files? I just can not find a solution for it. Does anyone got a tip for me how to achieve it? Thank you very much in advance. </p>
  
  <p><strong>!UPDATE!</strong></p>
  
  <p>As SenseException pointed out below, that injecting a service locator into a service is a bad practice and instead I should either inject repository or entity manager into the service, I have now worked out the first working solution for the entity manager.</p>
  
  <p>For that I have updated 2 Files: module.php and LatestAdvertService.php</p>
  
  <p><strong>#5 module.php</strong></p>
  
  <pre><code>public function getServiceConfig()
  {
      return array(
              'factories' =&gt; array(
  
                      'Advert\Service' =&gt; function ($sl) {
                          $entityManager =   $sl-&gt;get('doctrine.entitymanager.orm_default');
                          $myService = new Service\LatestAdvertService();
                          $myService-&gt;setEntityManager($entityManager);
                          //or you can set repository
                          //$repository = $entityManager-&gt;getRepository('Advert\Entity\Advert');
                          //$myService-&gt;setRepository($repository);
                          return $myService;
                      },
  </code></pre>
  
  <p><strong>4. the Service: Advert\Service\LatestAdvertService .php</strong></p>
  
  <pre><code>namespace Advert\Service;
  
  use Advert\Entity\Advert as AdvertEntity;
  use Doctrine\ORM\EntityManager;
  
  class LatestAdvertService 
  {
  
  
    public function setEntityManager(EntityManager $entityManager)
    {
      $this-&gt;entityManager = $entityManager;
    }
  
    public function setRepository(Repository $repository) {
     $this-&gt;repository = $repository;
    }
  
  
    public function fetchSingleByAdvertType($advertType)
    {
  
      $random = $this-&gt;entityManager-&gt;getRepository('Advert\Entity\Advert')-&gt;findAdvertsByDate($advertType);  
  //  $random = $this-&gt;repository-&gt;findAdvertsByDate($advertType);
  
      return $random;
    }
  }
  </code></pre>
  
  <p>I have tried to inject the repository but get the following error message:</p>
  
  <p><strong><em>Argument 1 passed to Advert\Service\LatestAdvertService::setRepository() must be an instance of Advert\Service\AdvertRepository, instance of Advert\Repository\AdvertRepository given, called in</em></strong></p>
  
  <p>I will continue to find a solution for the repository injection and update when successful.</p>