重构 ZF2 ServiceLocatorAwareInterface 以在视图助手中与 ZF3 一起工作的最佳方法是什么?
What is the best way to refactor ZF2 ServiceLocatorAwareInterface to work with ZF3 in view helper?
我有来自 ZF2 的视图助手,由于 ServiceLocatorAwareInterface
弃用,它不再适用于 ZF3。
重构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;
}
}
我应该使用视图助手工厂来注入服务定位器吗?如果是,应该如何完成?
不,您应该不使用工厂来注入ServiceLocator
实例(从不),您应该直接注入依赖项。在您的情况下,您应该注入 SlidersTable
服务。你应该这样做:
1) 使您的 class 构造函数依赖于您的 SlidersTable
服务:
<?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) 创建一个注入依赖项的工厂:
<?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) 在 module.config.php
:
中注册你的视图助手
//...
'view_helpers' => array(
'factories' => array(
'Site\View\Helper\SlidersList' => 'Site\View\Helper\Factory\SlidersListFactory'
)
),
//...
我有来自 ZF2 的视图助手,由于 ServiceLocatorAwareInterface
弃用,它不再适用于 ZF3。
重构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;
}
}
我应该使用视图助手工厂来注入服务定位器吗?如果是,应该如何完成?
不,您应该不使用工厂来注入ServiceLocator
实例(从不),您应该直接注入依赖项。在您的情况下,您应该注入 SlidersTable
服务。你应该这样做:
1) 使您的 class 构造函数依赖于您的 SlidersTable
服务:
<?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) 创建一个注入依赖项的工厂:
<?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) 在 module.config.php
:
//...
'view_helpers' => array(
'factories' => array(
'Site\View\Helper\SlidersList' => 'Site\View\Helper\Factory\SlidersListFactory'
)
),
//...