Symfony2中的依赖注入到底有什么作用? [英] What does exactly Dependency Injection in Symfony2 do?

查看:68
本文介绍了Symfony2中的依赖注入到底有什么作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注册了我的services.yml文件,如下所示:

I registered my services.yml file like below :

services:
  PMI.form.users_tasks:
        class: PMI\UserBundle\Form\UsersTasksType
        arguments: 
             EntityManager: "@doctrine.orm.default_entity_manager"

我可以通过 php app / console container:debug 列出它,这意味着我的服务已正确注册。

I can list it by php app/console container:debug, so that mean my service is registered properly.

在我的UsersTasksType类中,如下所示:

In my UsersTasksType class I have like below :

class UsersTasksType extends AbstractType
{

    protected $ur;

    public function __construct(EntityManager  $ur )
    {
        $this->setUr($ur);
    }

    // Get and setters
}

依赖注入是否意味着我不必再将 EntityManager 传递给类构造函数了?或者是什么 ?

Does Dependency Injection mean that I don't have to pass the EntityManager to the class constructor anymore? Or what ?

因为我必须运行以下代码:

Because when I have to run the code below :

$form   = $this->createForm(new UsersTasksType(), $entity);

我收到此错误:

Catchable Fatal Error: Argument 1 passed to PMI\UserBundle\Form\UsersTasksType::__construct() must be an instance of Doctrine\ORM\EntityManager, none given, called in C:\wamp\www\PMI_sf2\src\PMI\UserBundle\Controller\UsersTasksController.php on line 74 and defined in C:\wamp\www\PMI_sf2\src\PMI\UserBundle\Form\UsersTasksType.php line 19

我必须做以下事情:

$em = $this->container->get('doctrine.orm.entity_manager');
$form   = $this->createForm(new UsersTasksType($em), $entity);

那么依赖注入的全部目的是什么?

So what would be the whole purpose of Dependency Injection ?

推荐答案

依赖注入基本上使一个服务(在这种情况下,您的UserTasksType)可以访问另一服务(在这种情况下,您的

Dependency Injection basically gives one service (in this case, your UserTasksType) access to another service (in this case, your the entity manager).

arguments: 
     EntityManager: "@doctrine.orm.default_entity_manager"

这两行告诉Symfony希望在实例化新的UserTasksType对象时将实体管理器服务传递到构造函数中

These two lines tell Symfony to expect the entity manager service to be passed into the constructor when you instantiate a new UserTasksType object, which effectively gives your UserTasksType access to the entity manager.

如果您不在UserTasksType中使用实体管理器,则无需将其注入构造函数中,并且可以删除上面的两行以及UserTasksType中的 __ construct() / setUr()方法。

If you aren't using the entity manager in your UserTasksType, there is no need to inject it in the constructor and you could get rid of the two lines above and the __construct() / setUr() methods in your UserTasksType.

一个更好的示例可以帮助您理解DIC,可能是您有专门为发送电子邮件而编写的服务(例如,Swiftmail),并且您需要注射将其添加到另一个服务中,以便该服务可以发送电子邮件。

A better example to help you understand DIC might be that you have a service that is written specifically to send emails (Swiftmail, for e.g.) and you need to inject it into another service so that service can send emails.

通过添加

arguments: [ @mailer ]

对于您的服务定义,您的服务构造函数将期望您的邮件程序服务

to your service definition, your services constructor will expect your mailer service

__construct ($mailer)
{
    $this->mailer = $mailer;
}

这将使其可以发送电子邮件

which will give it access to send emails

someFunction()
{
    //do something useful, then send an email using the swift mailer service
    $this->mailer->sendEmail();
}

查看最新的Symfony文档以获取更多解释。

Check out the latest Symfony docs for more of an explanation.

http://symfony.com/doc/ current / book / service_container.html

这篇关于Symfony2中的依赖注入到底有什么作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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