Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在控制器外部使用serviceLocator_Php_Zend Framework_Zend Framework2 - Fatal编程技术网

Php 如何在控制器外部使用serviceLocator

Php 如何在控制器外部使用serviceLocator,php,zend-framework,zend-framework2,Php,Zend Framework,Zend Framework2,如何在模型中使用Zend Framework服务定位器?我有一个类,我想在其中使用表网关模型。我遵循了唱片集示例,希望访问控制器外部的表。但是,如果我将代码从控制器复制并粘贴到我需要的类中,我会得到一个错误(未定义的方法:getServiceLocator()。如何在控制器之外使用此“类” 最后,我想在控制器(在本例中是另一个类)之外的其他对象中访问“class AlbumTable”中的函数。谢谢 class Calendar implements ServiceLocatorAwareInt

如何在模型中使用Zend Framework服务定位器?我有一个类,我想在其中使用表网关模型。我遵循了唱片集示例,希望访问控制器外部的表。但是,如果我将代码从控制器复制并粘贴到我需要的类中,我会得到一个错误(未定义的方法:getServiceLocator()。如何在控制器之外使用此“类”

最后,我想在控制器(在本例中是另一个类)之外的其他对象中访问“class AlbumTable”中的函数。谢谢

class Calendar implements ServiceLocatorAwareInterface{ 

    protected $serviceLocator;

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

public function getServiceLocator()
{
    return $this->serviceLocator;
}
/*
 * Create Calendar Sync Table
 */
 public function getCalendarSyncTable()
 {
     if (!$this->calendarSyncTable) {
         $sm = $this->getServiceLocator();
         $this->calendarSyncTable = $sm->get('Pro\Model\CalendarSync\CalendarSyncTable');
     }
     return $this->calendarSyncTable;  
 }   
需要将控制器中的调用方式更改为

$calendar = $this->getServiceLocator()>get('Pro\Model\GoogleCalendar\Calendar');

如果您想在任何类中使用ServiceLocator,只需实现
ServiceLocatorAwareInterface
。例如:

class SomeClass implements ServiceLocatorAwareInterface
{
    protected $serviceLocator;

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

    public function getServiceLocator()
    {
        return $this->serviceLocator;
    }
ZendFramework2将自动将ServiceLocator的实例注入到类中。
阅读有关ServiceManager的更多信息

如果您想在任何类中使用ServiceLocator,只需实现
ServiceLocatorAwareInterface
。例如:

class SomeClass implements ServiceLocatorAwareInterface
{
    protected $serviceLocator;

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

    public function getServiceLocator()
    {
        return $this->serviceLocator;
    }
ZendFramework2将自动将ServiceLocator的实例注入到类中。
阅读有关ServiceManager的更多信息

我在转储ServiceManager时得到一个空值。我已经在上面发布了我的代码。我还缺少其他步骤吗?如何调用
Calendar
?如果只编写
new Calendar()
,则是,
$this->serviceLocator
将为空。您需要将您的
日历添加到ServiceManager配置中,如
'invokables'=>array('Calendar'=>'Application\Service\Calendar')
,然后这个魔术就可以工作了。查看更多详细信息是否在该阵列中?到目前为止,我只将module.conig用于路由和控制器控制器'=>数组('invokables'=>数组('Pro\Model\GoogleCalendar\Calendar'=>'Pro\Model\GoogleCalendar\Calendar')在此数组中?'service\u manager'=>数组('abstract\u factories'=>数组('Pro\Model\GoogleCalendar\Calendar'=>'Pro\Model\GoogleCalendar\Calendar'))是的,您应该使用
service\u manager
键。检查示例我在转储service manager时得到一个空值。我已在上面发布了我的代码。是否缺少其他步骤?如果您只编写
new Calendar(),如何调用
Calendar
,则是,
$this->serviceLocator
将为空。您需要将您的
日历添加到ServiceManager配置中,如
'invokables'=>数组('Calendar'=>'Application\Service\Calendar')
,那么这个魔法就可以发挥作用了。检查更多详细信息它是否在这个数组中?到目前为止,我只将module.conig用于路由和控制器。'controllers'=>数组('invokables'=>数组('Pro\Model\GoogleCalendar\Calendar'=>'Pro\Model\GoogleCalendar\Calendar')在这个数组中?'service\u manager'=>数组('abstract\u factories'=>array('Pro\Model\GoogleCalendar\Calendar'=>Pro\Model\GoogleCalendar\Calendar')是的,您应该使用
服务管理器
键。检查示例