• <bdo id='A8gQK'></bdo><ul id='A8gQK'></ul>
      <legend id='A8gQK'><style id='A8gQK'><dir id='A8gQK'><q id='A8gQK'></q></dir></style></legend>
    1. <tfoot id='A8gQK'></tfoot>
      <i id='A8gQK'><tr id='A8gQK'><dt id='A8gQK'><q id='A8gQK'><span id='A8gQK'><b id='A8gQK'><form id='A8gQK'><ins id='A8gQK'></ins><ul id='A8gQK'></ul><sub id='A8gQK'></sub></form><legend id='A8gQK'></legend><bdo id='A8gQK'><pre id='A8gQK'><center id='A8gQK'></center></pre></bdo></b><th id='A8gQK'></th></span></q></dt></tr></i><div id='A8gQK'><tfoot id='A8gQK'></tfoot><dl id='A8gQK'><fieldset id='A8gQK'></fieldset></dl></div>

      <small id='A8gQK'></small><noframes id='A8gQK'>

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

      时间:2024-08-14

      1. <small id='Bj2pP'></small><noframes id='Bj2pP'>

          <i id='Bj2pP'><tr id='Bj2pP'><dt id='Bj2pP'><q id='Bj2pP'><span id='Bj2pP'><b id='Bj2pP'><form id='Bj2pP'><ins id='Bj2pP'></ins><ul id='Bj2pP'></ul><sub id='Bj2pP'></sub></form><legend id='Bj2pP'></legend><bdo id='Bj2pP'><pre id='Bj2pP'><center id='Bj2pP'></center></pre></bdo></b><th id='Bj2pP'></th></span></q></dt></tr></i><div id='Bj2pP'><tfoot id='Bj2pP'></tfoot><dl id='Bj2pP'><fieldset id='Bj2pP'></fieldset></dl></div>
              <tbody id='Bj2pP'></tbody>
              <tfoot id='Bj2pP'></tfoot>
              <legend id='Bj2pP'><style id='Bj2pP'><dir id='Bj2pP'><q id='Bj2pP'></q></dir></style></legend>
              • <bdo id='Bj2pP'></bdo><ul id='Bj2pP'></ul>
                本文介绍了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() 何时不使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:如何在 yii 框架中自动设置 url 友好 下一篇:为什么 codeigniter 购物车类不允许名称中包含任何特殊字符?

                相关文章

              • <legend id='4hnVG'><style id='4hnVG'><dir id='4hnVG'><q id='4hnVG'></q></dir></style></legend>

                <small id='4hnVG'></small><noframes id='4hnVG'>

                  <bdo id='4hnVG'></bdo><ul id='4hnVG'></ul>
              • <tfoot id='4hnVG'></tfoot>
              • <i id='4hnVG'><tr id='4hnVG'><dt id='4hnVG'><q id='4hnVG'><span id='4hnVG'><b id='4hnVG'><form id='4hnVG'><ins id='4hnVG'></ins><ul id='4hnVG'></ul><sub id='4hnVG'></sub></form><legend id='4hnVG'></legend><bdo id='4hnVG'><pre id='4hnVG'><center id='4hnVG'></center></pre></bdo></b><th id='4hnVG'></th></span></q></dt></tr></i><div id='4hnVG'><tfoot id='4hnVG'></tfoot><dl id='4hnVG'><fieldset id='4hnVG'></fieldset></dl></div>