ZF2 ServiceManager

时间:2013-10-03 13:38:03

标签: php zend-framework2

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

1) classServiceManager.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....

2 个答案:

答案 0 :(得分: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');

答案 1 :(得分:0)

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

  1. 不要命名类classServiceManager这必然会在理解代码时产生冲突。
  2. 您的classServiceManager并没有神奇地注入ZF2的ServiceManager。只要从ServiceManager调用类,就会发生注入。
  3. 所以基本上你需要做的是:

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

    这样你的ServiceManager就可以从ZF2的ServiceManager调用,然后你的ServiceManager会知道ZF2的注释。

    HOWEVER :您的ServiceManager将不知道关于__construct()的内容。注入通过SetterInjection进行,因此必须首先创建Object,然后才能调用setter。

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