Zend Framework 2 Service in View Helper

I need to write a view assistant who receives the service and do something with it. I have successfully implemented a view helper to have access to a service locator. The problem is that the service I want to get was not found through the service locator when the __invoke method was called.

Subview Code:

<?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->getServiceLocator());

        return $model->getCurrent();

    }

}

Model code excerpt:

namespace Application\Model;

use Application\Entity,
    Andreatta\Model\Base as Base;

class Customer extends Base
{

    /**
     * 
     * @return Zend\Authentication\AuthenticationService
     */
    public function getAuthService()
    {

        $serviceLocator = $this->getServiceLocator();

        return $serviceLocator->get('Application\Auth');

    }

    /**
     * 
     * @return Zend\Authentication\Adapter\AdapterInterface
     */
    protected function getAuthAdapter()
    {

        return $this->getAuthService()->getAdapter();

    }

    public function getCurrent()
    {

        $authService = $this->getAuthService();

        if ($authService->hasIdentity())
            return $authService->getIdentity();

        return null;

    }

Excerpt from the .config.php module:

'service_manager' => array
(

    'factories' => array
    (

        'Application\Auth' => function($sm)
        {

            $authService = $sm->get('doctrine.authenticationservice.application');
            $authService->setStorage( new \Zend\Authentication\Storage\Session('Application\Auth'));

            return $authService;

        },

    ),

),

'view_helpers' => array
(

    'invokables' => array
    (

        'loggedCustomer' => 'Application\View\Helper\LoggedCustomer',

    ),

),

When I call the view helper from any view, I get the following:

Zend\View\HelperPluginManager::get was unable to fetch or create an instance for Application\Auth

The strange thing is that the application works correctly (i.e. this service is usually used by other parts of the application).

EDIT:

, , , , , view_manager module.config.php. - , ?

+4
5

$this->getServiceLocator() , $this->getServiceLocator()->getServiceLocator()

+13

@rafaame: Helper

:

$this->getView()->getHelperPluginManager()->getServiceLocator(); 

:

namespace Tmcore\View\Helper;

use Zend\View\Helper\AbstractHelper;

class Resource extends AbstractHelper
{
    public function adminResource()
    {
        $sm = $this->getView()->getHelperPluginManager()->getServiceLocator();
        $adminConfig = $sm->get('ModuleManager')->loadModule('admin')->getConfig();
        return $adminConfig;
    }
}
+7

, Zend\View\HelperPluginManager ServiceManager. , , .

, LoggedCustomer, SM. , ServiceLocatorAwareInterface, SM , .

UPDATE:

, , ServiceLocatorAwareTrait; .

, http://framework.zend.com/manual/2.0/en/modules/zend.service-manager.quick-start.html

Zend Framework MVC , ServiceManager, Zend\ServiceManager\ServiceLocatorInterface, Zend\ServiceManager\ServiceLocatorAwareInterface. .

, ... ServiceLocatorAwareInterface .

, .

, , factory Module.php , invokable . :

   public function getViewHelperConfig()
   {
    return array(
        'factories' => array(
                'loggedCustomer' => function($sm) {


                     $vh = new View\Helper\LoggedCustomer();
                     $vh->setServiceLocator($sm->getServiceLocator());

                      return $vh;

                  }
             );
      }

, , ServiceLocatorAwareInterface, .

+3

, , , , "view_manager" .

"" , factory :

'view_helpers' => 
[

    'factories' =>
    [

        'loggedCustomer' => function($pluginManager)
        {

            $serviceLocator = $pluginManager->getServiceLocator();

            $viewHelper = new View\Helper\LoggedCustomer();
            $viewHelper->setServiceLocator($serviceLocator);

            return $viewHelper;

        },

    ]

],

But you have to make sure that you view it in the setServiceLocator method in the view helper. Otherwise, the "limited" service manager will subsequently be added to the view assistant. Like this:

public function setServiceLocator(ServiceLocatorInterface $serviceLocator)
{

    if($this->serviceLocator !== null)
        return $this;

    $this->serviceLocator = $serviceLocator;

    return $this;
}

It fixes the problem, but for me it's a terrific hack.

+1
source

As helpers, if you want to access applications, use

$this->getServiceLocator()->getServiceLocator()
0
source

All Articles