title: Zend Framework 2 Service in View Helper
body: <p>I need to write a view helper that gets a service and do something with it. I successfully implemented the view helper to have access to the service locator. The problem is that the service I want to get is not being found through the service locator when the __invoke method is called.</p>
  
  <p>The view helper code:</p>
  
  <pre><code>&lt;?php
  
  namespace Application\View\Helper;
  
  use Zend\View\Helper\AbstractHelper,
      Zend\ServiceManager\ServiceLocatorAwareInterface,
  
      Application\Model;
  
  class LoggedCustomer extends AbstractHelper implements ServiceLocatorAwareInterface
  {
  
      use \Zend\ServiceManager\ServiceLocatorAwareTrait;
  
      public function __invoke()
      {
  
          $model = new Model\Customer($this-&gt;getServiceLocator());
  
          return $model-&gt;getCurrent();
  
      }
  
  }
  </code></pre>
  
  <p>A snippet of the model code:</p>
  
  <p>
  
  <pre><code>namespace Application\Model;
  
  use Application\Entity,
      Andreatta\Model\Base as Base;
  
  class Customer extends Base
  {
  
      /**
       * 
       * @return Zend\Authentication\AuthenticationService
       */
      public function getAuthService()
      {
  
          $serviceLocator = $this-&gt;getServiceLocator();
  
          return $serviceLocator-&gt;get('Application\Auth');
  
      }
  
      /**
       * 
       * @return Zend\Authentication\Adapter\AdapterInterface
       */
      protected function getAuthAdapter()
      {
  
          return $this-&gt;getAuthService()-&gt;getAdapter();
  
      }
  
      public function getCurrent()
      {
  
          $authService = $this-&gt;getAuthService();
  
          if ($authService-&gt;hasIdentity())
              return $authService-&gt;getIdentity();
  
          return null;
  
      }
  </code></pre>
  
  <p>The snippet from module.config.php:</p>
  
  <pre><code>'service_manager' =&gt; array
  (
  
      'factories' =&gt; array
      (
  
          'Application\Auth' =&gt; function($sm)
          {
  
              $authService = $sm-&gt;get('doctrine.authenticationservice.application');
              $authService-&gt;setStorage( new \Zend\Authentication\Storage\Session('Application\Auth'));
  
              return $authService;
  
          },
  
      ),
  
  ),
  
  'view_helpers' =&gt; array
  (
  
      'invokables' =&gt; array
      (
  
          'loggedCustomer' =&gt; 'Application\View\Helper\LoggedCustomer',
  
      ),
  
  ),
  </code></pre>
  
  <p>When calling the view helper from any view I get the following:</p>
  
  <pre><code>Zend\View\HelperPluginManager::get was unable to fetch or create an instance for Application\Auth
  </code></pre>
  
  <p>The weird is that the application is functioning correctly (i.e. this service is being normally used by other parts of the application).</p>
  
  <p>EDIT:</p>
  
  <p>I did some research and I think the only services that I can access through the service manager inside the view helper are the ones registered inside the 'view_manager' section of module.config.php. Does anyone have an idea of how to access the other services?</p>