1

I have view helper from ZF2 that doesn't work anymore with ZF3 because of ServiceLocatorAwareInterface deprecation.

What's the proper way to refactor that class:

<?php

namespace Site\View\Helper;

use Zend\View\Helper\AbstractHelper;
use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

class SlidersList extends AbstractHelper implements ServiceLocatorAwareInterface 
{
    protected $_serviceLocator;

    public function __invoke() 
    {
        return $this->getView()->render(
            'partials/sliders', 
            array('sliders' => $this->getServiceLocator()->getServiceLocator()->get('Sliders/Model/Table/SlidersTable')->fetchAll(true))
        );
    }

    public function setServiceLocator(ServiceLocatorInterface $serviceLocator)
    {
        $this->_serviceLocator = $serviceLocator;
    }

    public function getServiceLocator() 
    {
        return $this->_serviceLocator;
    }
}

Should I use view helper factory to inject service locator and if yes, how it should be done?

Wilt
  • 41,477
  • 12
  • 152
  • 203
Botman
  • 81
  • 7
  • why would you need the service locator in your view helper ? The problem is here, not on how to give the service locator to your view helper. – Unex Sep 12 '16 at 15:36
  • 1
    I've provided an answer where [you create a factory class to resolve dependencies](http://stackoverflow.com/questions/38696620/zf3-controller-not-able-to-access-the-model-class-table-located-in-another-modul/38703223#38703223). The question is for a controller rather than a view helper, however the process is still the same. – AlexP Sep 12 '16 at 17:07

1 Answers1

3

No, you should NOT use a factory to inject a ServiceLocator instance (never), instead you should inject the dependencies directly. In your case you should inject your SlidersTable service. You should do like this:

1) Make your class constructor dependent on your SlidersTable service:

<?php

namespace Site\View\Helper;

use Zend\View\Helper\AbstractHelper;
use Sliders\Model\Table\SlidersTable;

class SlidersList extends AbstractHelper
{
    protected $slidersTable;

    public function __construct(SlidersTable $slidersTable) 
    {
        return $this->slidersTable = $slidersTable;
    }

    public function __invoke() 
    {
        return $this->getView()->render(
            'partials/sliders', 
            array('sliders' => $this->slidersTable->fetchAll(true))
        );
    }
}

2) Create a factory where you inject your dependency:

<?php
namespace Site\View\Helper\Factory;

use Site\View\Helper\SlidersList;
use Interop\Container\ContainerInterface;
use Zend\ServiceManager\Factory\FactoryInterface;

class SlidersListFactory implements FactoryInterface
{
    /**
     * @param ContainerInterface $container
     * @param string $requestedName
     * @param array|null $options
     * @return mixed
     */
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
    {
        $serviceManager = container
        $slidersTable= $container->get('Sliders/Model/Table/SlidersTable');
        return new SlidersList($slidersTable);
    }
}

3) register your view helper inside your module.config.php:

//...

'view_helpers' => array(
    'factories' => array(
        'Site\View\Helper\SlidersList' =>  'Site\View\Helper\Factory\SlidersListFactory'
    )
),

//...
Wilt
  • 41,477
  • 12
  • 152
  • 203