4

我尝试在没有 MVC 模块的情况下使用来自 zf2 的 ServiceManager。我有两个文件:classServiceManager.php 和 sm.php。

1)类ServiceManager.php:

namespace ZF2;

use Zend\ServiceManager\ServiceManager; 
use Zend\ServiceManager\ServiceLocatorAwareInterface; 
use Zend\ServiceManager\ServiceLocatorInterface; 
use \InvalidArgumentException;  

class classServiceManager implements ServiceLocatorAwareInterface{
    protected $serviceLocator;

    function __construct(){
        //echo "SM: ". $this->getServiceLocator()->get('sm');
        echo "SM: ".$this->serviceLocator->get('sm');

    }

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

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

}

2) sm.php

$config = array(...);

require_once 'Zend/Loader/AutoloaderFactory.php';
Zend\Loader\AutoloaderFactory::factory($config); 
......  
use ZF2\classServiceManager;

$serviceManager = new ServiceManager();
$serviceManager->setService('sm', 'aaa');   

$a = new classServiceManager();

但是当我运行时sm.php,我得到一个错误:

Fatal error: Call to a member function get() on a non-object....
4

2 回答 2

3

默认情况下,Zend Framework MVC 注册一个初始化器,它将注入 ServiceManager 实例,它是 Zend\ServiceManager\ServiceLocatorInterface 的实现,到任何实现 Zend\ServiceManager\ServiceLocatorAwareInterface 的类中。

所以 ServiceLocatorAwareInterface 只用在 MVC 应用程序中。

如果你想在没有 MVC 的情况下使用 zf2 的 ServiceManager,你需要自己创建一个 ServiceManager

这是我的

namespace Westdc\Service;

use Zend\ServiceManager\ServiceManager as Zend_ServiceManager;

class ServiceManager {

private $serviceManager;

function __construct()
{
    $this->serviceManager = new Zend_ServiceManager;
    $this->serviceManager->addAbstractFactory(new ServiceFactory);

    $configService = $this->serviceManager->get('ConfigService');
    $invoked_services = $configService->get('service.invoked.ini');

    foreach($invoked_services as $k=>$v) {
        $this->serviceManager->setInvokableClass($k, $v);
    }
}

public function addKey($key,$value = "")
{
    if(!empty($value))
        $this->serviceManager->$key($value);
    else
        $this->serviceManager->$key();
}

public function setServiceManager(Zend_ServiceManager $service)
{
    $this->serviceManager = $service;
}

public function getServiceManager()
{
    return $this->serviceManager;
}

} 

这是我使用它的例子

use Westdc\Service\ServiceManager;

$serviceManager = new ServiceManager();
$serviceManager = $serviceManager->getServiceManager();

$authService = $serviceManager->get('Auth');
于 2014-12-30T14:32:02.400 回答
0

现在我不太确定我的答案是否 100% 正确,但你正在做一些奇怪的事情;)

  1. 不要命名一个类classServiceManager,这势必会在理解代码时产生冲突。
  2. classServiceManager不会神奇地注入 ZF2 的 ServiceManager。每当从 ServiceManager 调用一个类时,就会发生注入。

所以基本上你需要做的是:

$originalSM = new ServiceManager();
$originalSM->setService('csm', 'ZF2\classServiceManager');

$a = $orignalSM->get('csm');

这样,您的 ServiceManager 将从 ZF2 的 ServiceManager 调用,然后您的 ServiceManager 将知道 ZF2,因为它将被注入。

但是:您的 ServiceManager不会__construct(). 注入通过 SetterInjection 发生,因此必须先创建对象,然后才能调用 setter。

我的解释可能有点奇怪,但我希望你可以利用它。

于 2013-10-03T13:46:14.570 回答