ZF2何时使用getServiceLocator()以及何时不使用 [英] ZF2 when to use getServiceLocator() and when not to

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

问题描述

我对何时使用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 Helloworld\Service;

use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;


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 Helloworld\Controller;

use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

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(
        'Helloworld\Controller\Index' => 'Helloworld\Controller\IndexControllerFactory'
    )
)
,
'service_manager' => array(
    'invokables' => array(
        'loggingService' => 'Helloworld\Service\LoggingService'
    ),
    'factories' => array(
        'greetingService'=> 'Helloworld\Service\GreetingServiceFactory'
    ),
)

请您澄清一下:)

祝你有美好的一天!

推荐答案

方法getServiceLocator

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 (
                    \Zend\ServiceManager\AbstractPluginManager $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()以及何时不使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆