创建表单作为服务(而不仅仅是一个classType)的优势? [英] Advantage of creating a form as a service (rather than just a classType)?

查看:117
本文介绍了创建表单作为服务(而不仅仅是一个classType)的优势?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在这里和那里读过,你可以创建一个表单作为服务。但我真的没有看到这样做的重点。



任何人都可以给我是这样做的优点的一个简单例子,而不仅仅是将表单定义为一个类。



没有代码(或不多),只是一个快速具体例子,我将以表格作为一种服务来做,如果它是一个简单的类,我不能这样做。






这里我列出的优点(或不是):

依赖注入(不需要声明为服务实际上):

表单作为一项服务将允许您向其中注入其他服务并获得它们的构思。 ==>但实际上不需要将表单声明为服务来执行此操作: div>


  1. 使用案例1:使用案例1: 您的表单需要一些其他服务



    解决方案:通过将其声明为服务,您不需要记住类每次都有名字,而且你不必访问容器来将服务传递给你的表单实例,所以不用:

      $ this-> createForm('Answer',null,array('service'=> $ service))

    你做了 $ this-> createForm('Answer','my_form')



    和你得到正确配置的表单。当您更改表单时,您不必在以后的代码中随处更改它。它也更容易测试,当你不必每次都将所有内容传递给构造函数时,它更难以忘记。


    1. 使用案例2

    案例:您将表单嵌入其他表单中

    解决方案:您无法在表单中访问Dep.Container(并且您不应该),因此无法执行操作

      $ builder-> add('Answer',new MyForm($ service1,$ service2))

    里面的表单,而不是你做的

      $ builder-> add ('Answer','my_form')




    1. 使用案例3

    案例:其他人使用您的代码,您的表单可能会在不同情况下发生变化



    解决方案:如果您将表单声明为服务,则可以将其类设置为参数,以防其他人想要扩展表单,他可以通过使用parameters.yml来完成,而不会干扰你的代码。一些示例可能是捆绑包,如FOSUserBundle,只要使用配置就可以替换不同形式的User类。


    I have read here and there, that you could create a form as a service.But I do not really see the point of doing so.

    Could anyone give me a quick example of the advantage of doing so rather than just having a form define as a class.

    No code (or not much) but just a quick concrete exemple of what I would do with form as a service that I couldn't do if it was a simple class.


    Here the advantage (or not) that I listed so far:

    dependancy injection (no need to be declare as a service in fact):
    Form as a service would allow you to inject other services into it and get them thought constructor. ==> but actually no need to declare a form as a service to do so:
    here an exemple of a formType (not define as a service) using an external service.

    解决方案

    1. Use case 1:

    Case: you use your form in several places, your form requires some other services

    solution: By declaring it as a service, you dont need to remember the class name each time, and you dont have to access the container to pass the services to the your form instance, so instead of:

    $this->createForm('Answer', null, array('service' => $service))
    

    you do $this->createForm('Answer', 'my_form')

    and you get properly configured form. When you change the form, you dont have to change it everywhere in your code later. Its also easier to test and its harder to forget something when you dont have to pass everything to the constructor each time.

    1. Use case 2

    Case: You embed your form into other forms

    Solution: You dont have access to the Dep.Container in your form(and you shouldnt have) so its impossible to do

    $builder->add('Answer', new MyForm($service1,$service2))
    

    inside forms, instead you do

     $builder->add('Answer', 'my_form')
    

    1. Use case 3

    Case: Others use your code, you form might change in different cases

    Solution: If you declare your form as a service, you can set its class to be a parameter, so in case somebody else wants to extend you form, he can do it just by using parameters.yml, without messing with your code. Some examples might be bundles like FOSUserBundle, when you can replace the User class of different forms just by using configuration.

    这篇关于创建表单作为服务(而不仅仅是一个classType)的优势?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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