ZF2:等效于Zend形式的getServiceLocator [英] ZF2: Equivalent of getServiceLocator in Zend Form

查看:83
本文介绍了ZF2:等效于Zend形式的getServiceLocator的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设: Event\Service\EventService 是我的个人对象,可与 Event\Entity\Event 实体

assumption: Event\Service\EventService is my personal object that works with Event\Entity\Event entities

此代码在ActionController中起作用:

This code works in an ActionController:

$eventService = $this->getServiceLocator()->get('Event\Service\EventService');

如何在 b中获取 $ eventService Zend\Form\Form 以同样的方式?

How can I get $eventService in a Zend\Form\Form in the same way?

推荐答案

如果您有这样的依赖项,则有两个选择。在您的情况下,表格取决于服务。第一种选择是注入依赖项

You have two options if you have a dependency like this. In your case, a Form depends on a Service. The first option is to inject dependencies:

class Form
{
  protected $service;
  public function setService(Service $service)
  {
    $this->service = $service;
  }
}

$form = new Form;
$form->setService($service);

在这种情况下, $ form 为没有意识到 $ service 的位置,通常认为这是个好主意。为了确保您不需要每次需要 Form 时都自己设置所有依赖项,可以使用服务管理器来创建工厂。

In this case, the $form is unaware of the location of $service and generally accepted as a good idea. To make sure you don't need to set up all the dependencies yourself each time you need a Form, you can use the service manager to create a factory.

创建工厂的一种方法(还有更多方法)是在模块类中添加 getServiceConfiguration()方法并使用关闭以实例化 Form 对象。这是将服务注入 Form 的示例:

One way (there are more) to create a factory is to add a getServiceConfiguration() method to your module class and use a closure to instantiate a Form object. This is an example to inject a Service into a Form:

public function getServiceConfiguration()
{
    return array(
        'factories' => array(
            'Event\Form\Event' => function ($sm) {
                $service = $sm->get('Event\Service\EventService');
                $form    = new Form;
                $form->setService($service);

                return $form;
            }
        )
    );
}

然后,您只需获取表格来自您的服务经理。例如,在您的控制器中:

Then you simply get the Form from your service manager. For example, in your controller:

$form = $this->getServiceLocator()->get('Event\Form\Event');

第二种选择是拉依赖关系。尽管不建议将此类用于表单之类,但是您可以注入服务管理器,以便表单可以自己提取依赖项:

A second option is to pull dependencies. Though it is not recommended for classes like forms, you can inject a service manager so the form can pull dependencies itself:

class Form
{
    protected $sm;

    public function setServiceManager(ServiceManager $sm)
    {
        $this->sm = $sm;
    }

    /**
     * This returns the Service you depend on
     *
     * @return Service
     */
    public function getService ()
    {
        return $this->sm->get('Event\Service\EventService');
    }
}

但是,第二个选择将代码与不必要的耦合耦合在一起而且很难测试您的代码。因此,请使用依赖注入而不是自己拉依赖。在少数情况下,您可能想自己拉依赖项:)

However, this second option couples your code with unnecessary couplings and it makes it very hard to test your code. So please use dependency injection instead of pulling dependencies yourself. There are only a handful of cases where you might want to pull dependencies yourself :)

这篇关于ZF2:等效于Zend形式的getServiceLocator的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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