Zend Framework 2:视图助手中的数据库连接

mik*_*int 3 php zend-framework2

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

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

在我的模块类中,我有:

public function getViewHelperConfig()
{
    return array(
        'factories' => array(
            'liveStreaming' => function() {
                return new LiveStreaming();
            },
        ),
    );
}    
Run Code Online (Sandbox Code Playgroud)

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

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;
    }   
}
Run Code Online (Sandbox Code Playgroud)

所以,我想$events从数据库中获取数组.我创建Application\Model\LiveStreamingApplication\Model\LiveStreamingTable(继ZF2官方教程的说明),我需要一些帮助进行到下一步,这可能应该与服务定位器做.

Ale*_*exP 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;
  } 

}
Run Code Online (Sandbox Code Playgroud)

工厂变成:

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);
            },
        ),
    );
}   
Run Code Online (Sandbox Code Playgroud)

选项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;
  } 

}
Run Code Online (Sandbox Code Playgroud)

您的工厂将如下所示:

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);
            },
        ),
    );
}  
Run Code Online (Sandbox Code Playgroud)

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

编辑

确保您已将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);
     },

   ),
 );
}
Run Code Online (Sandbox Code Playgroud)