3

我发现了一些与此问题相关的其他帖子,但是我无法实现我想要的,所以我决定删除所有内容并在一些帮助下重新开始......

到目前为止,这是我的工作,它完成了这项工作,但数据是硬编码在一个数组中提供的,我需要创建一个数据库连接来获取这些数据。

在我的模块类中,我有:

public function getViewHelperConfig()
{
    return array(
        'factories' => array(
            'liveStreaming' => function() {
                return new LiveStreaming();
            },
        ),
    );
}    

这是我在视图助手中的代码:

namespace Application\View\Helper;

use Zend\View\Helper\AbstractHelper;

class LiveStreaming extends AbstractHelper
{ 

    protected $liveStreamingTable;

    public function __invoke()
    {
        $events = array(
            '1' => array('name' => 'Event name', 
                    'sport' => 'Soccer', 
                    'time' => '11:30'), 
            '2' => array('name' => 'Event name', 
                    'sport' => 'Soccer', 
                    'time' => '17:00'),             
        );
        return $events;
        //this is what should be used (or something like that) to get the data from the db...
        //return array('events' => $this->getLiveStreamingTable()->fetchAll() ); 
    }

    public function getLiveStreamingTable()
    {
    if (!$this->liveStreamingTable) {
           $sm = $this->getServiceLocator();
           $this->liveStreamingTable = $sm->get('LiveStreaming\Model\LiveStreamingTable');
       }
       return $this->liveStreamingTable;
    }   
}

所以,我想$events从数据库中获取数组。我已经创建Application\Model\LiveStreamingApplication\Model\LiveStreamingTable(按照 ZF2 官方教程的说明),我需要一些帮助才能进行下一步,这可能与服务定位器有关。

4

1 回答 1

7

你似乎快到了。唯一缺少的是$this->getServiceLocator();从视图助手中调用的能力(因为AbstractHelper不提供此方法)。

有两种选择

  • 直接注入LiveStreamingTable视图助手
  • 注入ServiceManager自身并LiveStreamingTable在助手内创建

选项 1建立视图助手LiveStreamingTable依赖项(构造函数中的类型提示)

namespace Application\View\Helper;

use Zend\View\Helper\AbstractHelper;
use LiveStreaming\Model\LiveStreamingTable;


class LiveStreaming extends AbstractHelper
{ 
  protected $liveStreamingTable;

  public function __construct(LiveStreamingTable $liveStreamingTable)
  {
    $this->liveStreamingTable = $liveStreamingTable;
  }

  public function getLiveStreamingTable()
  {
    return $this->liveStreamingTable;
  } 

}

工厂变成:

public function getViewHelperConfig()
{
    return array(
        'factories' => array(
            'liveStreaming' => function($sl) {
                // Get the shared service manager instance
                $sm = $sl->getServiceLocator(); 
                $liveStreamingTable = $sm->get('LiveStreaming\Model\LiveStreamingTable');
                // Now inject it into the view helper constructor
                return new LiveStreaming($liveStreamingTable);
            },
        ),
    );
}   

选项 2 - 实现ServiceLocatorAwareInterface(使其再次成为视图助手的依赖项)

namespace Application\View\Helper;

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

class LiveStreaming extends AbstractHelper implements ServiceLocatorAwareInterface
{ 
  protected $serviceLocator;

  protected $liveStreamingTable;

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

  public function setServiceLocator(ServiceLocatorInterface $serviceLocator);

  public function getServiceLocator();

  public function getLiveStreamingTable()
  {
    if (null == $this->liveStreamingTable) {
      $this->liveStreamingTable = $this->getServiceLocator()->get('LiveStreaming\Model\LiveStreamingTable');
    }
    return $this->liveStreamingTable;
  } 

}

您的工厂将如下所示:

public function getViewHelperConfig()
{
    return array(
        'factories' => array(
            'liveStreaming' => function($sl) {
                // Get the shared service manager instance
                $sm = $sl->getServiceLocator(); 
                // Now inject it into the view helper constructor
                return new LiveStreaming($sm);
            },
        ),
    );
}  

就个人而言,我觉得从依赖注入 (DI) 的角度来看,选项 1LiveStreamingTable更有意义 - 很明显,这是创建视图助手所需要的。

编辑

确保您LiveStreaming\Model\LiveStreamingTable还向服务管理器注册了服务(正如我们在上面的代码中要求的那样$sm->get('LiveStreaming\Model\LiveStreamingTable');

// Module.php
public function getServiceConfig()
{
 return array(
   'factories' => array(

     'LiveStreaming\Model\LiveStreamingTable' => function($sm) {

       // If you have any dependencies for the this instance
       // Such as the database adapter etc either create them here 
       // or request it from the service manager
       // for example:
       $foo = $sm->get('Some/Other/Registered/Service');
       $bar = new /Directly/Created/Instance/Bar();

       return new \LiveStreaming\Model\LiveStreamingTable($foo, $bar);
     },

   ),
 );
}
于 2013-11-05T12:52:22.187 回答