加入收藏 | 设为首页 | 会员中心 | 我要投稿 北几岛 (https://www.beijidao.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

php – ZF2 ServiceManager

发布时间:2021-08-28 03:46:51 所属栏目:大数据 来源: https://www.jb51.cc
导读:我尝试在没有MVC模块的情况下使用zf2中的ServiceManager.我有两个文件:classServiceManager.PHP和sm.PHP. 1)classServiceManager.PHP: namespace ZF2;use ZendServiceManagerServiceManager; use ZendServiceManagerServiceLocatorAwareInterface; use

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

1)classServiceManager.PHP:

namespace ZF2;

use ZendServiceManagerServiceManager; 
use ZendServiceManagerServiceLocatorAwareInterface; 
use ZendServiceManagerServiceLocatorInterface; 
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';
ZendLoaderAutoloaderFactory::factory($config); 
......  
use ZF2classServiceManager;

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

$a = new classServiceManager();

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

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

解决方法:

By default, the Zend Framework MVC registers an initializer that will inject the ServiceManager instance, which is an implementation of ZendServiceManagerServiceLocatorInterface, into any class implementing ZendServiceManagerServiceLocatorAwareInterface.

所以ServiceLocatorAwareInterface仅用于MVC应用程序.

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

这是我的

namespace WestdcService;

use ZendServiceManagerServiceManager 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 WestdcServiceServiceManager;

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

$authService = $serviceManager->get('Auth');

(编辑:北几岛)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读