沃梦达 / 编程问答 / php问题 / 正文

ZF2 何时使用 getServiceLocator() 何时不使用

ZF2 when to use getServiceLocator() and when not to(ZF2 何时使用 getServiceLocator() 何时不使用)

本文介绍了ZF2 何时使用 getServiceLocator() 何时不使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的很困惑何时使用 getServiceLocator 何时不使用.举个例子:

I am really confused on when to use getServiceLocator and when not to. As an example:

+ Module
-+ Helloworld
--+ src
---+ Controller
----+ IndexController.php
----+ IndexControllerFactory.php

---+ Service
----+ LogginService.php
----+ GreetingService.php
----+ GreetingServiceFactory.php

GreetingServiceFactory.php 内容如下:

GreetingServiceFactory.php has the content:

<?php
namespace HelloworldService;

use ZendServiceManagerFactoryInterface;
use ZendServiceManagerServiceLocatorInterface;


class GreetingServiceFactory implements FactoryInterface
{

    public function createService (ServiceLocatorInterface $serviceLocator)
    {
        $greetingService = new GreetingService();

        $greetingService->setEventManager($serviceLocator->get('eventManager'));

        $loggingService = $serviceLocator->get('loggingService');

        $greetingService->getEventManager()->attach('getGreeting', array(
            $loggingService,
            'onGetGreeting'
        ));

        return $greetingService;
    }
}

并且IndexControllerFactory.php有内容:

And IndexControllerFactory.php has the content:

<?php
namespace HelloworldController;

use ZendServiceManagerFactoryInterface;
use ZendServiceManagerServiceLocatorInterface;

class IndexControllerFactory implements FactoryInterface
{

    public function createService (ServiceLocatorInterface $serviceLocator)
    {
        $ctr = new IndexController();

        $ctr->setGreetingService($serviceLocator->getServiceLocator()
            ->get('greetingService'));
        return $ctr;
    }
}

如您所见,我的 ControllerFactory 中需要 $serviceLocator->getServiceLocator() 而我的 ServiceFactory 中不需要.为什么?两者都使用相同的接口 ServiceLocatorInterface,它甚至没有定义 getServiceLocator() 方法.

As you can see, I need $serviceLocator->getServiceLocator() in my ControllerFactory but not in my ServiceFactory. Why? Both use the same interface ServiceLocatorInterface which does not even define the getServiceLocator() method.

module.config.php:

module.config.php:

'controllers' => array(
    'factories' => array(
        'HelloworldControllerIndex' => 'HelloworldControllerIndexControllerFactory'
    )
)
,
'service_manager' => array(
    'invokables' => array(
        'loggingService' => 'HelloworldServiceLoggingService'
    ),
    'factories' => array(
        'greetingService'=> 'HelloworldServiceGreetingServiceFactory'
    ),
)

我会很感激任何澄清:)

I'd appreciate any clarification :)

祝你有美好的一天!

推荐答案

getServiceLocator方法定义在AbstractPluginManager,因为它实现了 ServiceLocatorAwareInterface.正如 Maks3w 指出的那样,它不是 ServiceLocatorInterface,所以在实现服务工厂时避免使用它.

The method getServiceLocator is defined on the AbstractPluginManager, since it implements the ServiceLocatorAwareInterface. As Maks3w pointed out, it is not part of the ServiceLocatorInterface, so avoid using it when implementing a service factory.

无论如何,您都可以将您的工厂定义为闭包并仍然使用它:

You can anyway define your factory as closure and still use it:

class MyModule
{
    public function getControllerConfig()
    {
        return array(
            'factories' => array(
                'IndexController' => function (
                    endServiceManagerAbstractPluginManager $pm
                ) {
                    $ctr = new IndexController();

                    $ctr->setGreetingService(
                        $pm
                            ->getServiceLocator()
                            ->get('greetingService')
                    );

                    return $ctr;
                },
            ),
        );
    }
}

虽然在此示例中 $pm 确实是一个 ServiceLocatorInterface 实例,但您仍需要获取对主"服务管理器的引用才能访问 'greetingService'.

While in this example $pm is indeed a ServiceLocatorInterface instance, you will still need to get a reference to the "main" service manager to access the 'greetingService'.

ZF2对控制器、服务、视图助手、控制器插件等使用不同的服务管理器或插件管理器……那主要是为了类型提示(看AbstractPluginManager的接口就明白了如何实现类型严格性)和安全性.

ZF2 uses different service managers or plugin managers for controllers, services, view helpers, controller plugins, etc... That is mainly for type-hinting (look at the interface of the AbstractPluginManager to understand how type strictness is achieved) and for security.

在这种情况下,安全问题是不允许访问非控制器的服务,尤其是带有动态 controller 参数的路由.这就是控制器保存在单独的插件管理器中的原因.

In this case, the security issue is disallowing access to services that are not controllers, especially with routes with a dynamic controller parameter. That's why controllers are kept in a separate plugin manager.

由于控制器插件管理器是从主"服务管理器创建的,因此它也通过 ServiceLocatorAwareInterface 进行初始化.

Since the controller plugin manager is created from the "main" service manager, it also is initialized thanks to the ServiceLocatorAwareInterface.

为了更清楚地说明这一点,我添加了关系图(不包括工厂并且不将其视为有效的 UML):

To make this more clear, I've added a graph of the relations (does not include the factory and don't take it as valid UML):

这篇关于ZF2 何时使用 getServiceLocator() 何时不使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:ZF2 何时使用 getServiceLocator() 何时不使用

基础教程推荐