Symfony2 MVC:我的代码在哪里? [英] Symfony2 MVC: where does my code belong?

查看:56
本文介绍了Symfony2 MVC:我的代码在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻求澄清是将代码放入控制器,实体还是提供服务.

I'm seeking clarification on whether to put code in a controller, an entity or to make a service.

我有"cardset"和"card"对象(后者中的许多嵌入在前者中,MongoDB中),这些对象由普通的PHP类/对象表示.这些包含例如'id','postal_address'.

I have 'cardset' and 'card' objects (where many of the latter are embedded in the former, MongoDB), represented by normal PHP classes/objects. These contain attributes e.g. 'id', 'postal_address'.

我有一种生成卡片PDF的方法.目前,我在"Card"对象中拥有该对象,因此可以从Controller调用:

I have a method that generates a PDF of a card. Currently I have that inside the 'Card' object so from a Controller I can call:

$card->makePDF()

对我来说这看起来很干净,但是我怀疑我错了.

That seems clean and OO to me, but I suspect I'm wrong.

如果我将所有逻辑放入冗长且笨拙的控制器中,并且我不确定控制器是否是对我的对象进行操作的方法的场所.那是为了什么服务?

If I put all the logic in the controller that gets long and unwieldy, and I'm not sure the controller is the place for methods that act on my objects. Is that what services are for?

尝试总结:一个对象应该知道它可以自己做的所有常规事情,并将它们作为成员函数包含在内部,或者应该将对象的其他方法传递给对象以对其进行操作.如果是这样,应该将这些方法保存在哪里?

To try and summarise: should an object know all the regular things it could do 'to itself' and have them inside as member functions, or should methods elsewhere be passed the object to act upon. If so, where should those methods be kept?

我非常确定它不是存储库",因为这似乎有助于检索/存储实体.

I'm pretty sure it's not a 'Repository' because that just seems to help retrieve/store entities.

谢谢!

推荐答案

简短答案:

PDF生成应该是服务,而不是对象上的方法.

Short answer:

PDF generation should be a service, not a method on an object.

通常,尤其是在Symfony2中,应仅使用模型来存储数据.控制器用于操纵模型之间的关系,视图用于以人类或计算机可读形式表示数据.服务是针对那些实际上不符合上述任何条件的事物的,而这些事物与您的Web应用程序的状态无关.

In general, and in Symfony2 especially, models should just be used to store data. Controllers are used to manipulate relationships between models, and Views are used to express the data in human- or computer-readable form. Services are for things that don't really fit into any of the above -- things that don't have to do with your web application's state.

一个很好的例子是发送电子邮件.电子邮件包含数据(模型).用户已发送电子邮件(控制器).电子邮件看起来有些特定(视图).但是,实际发送电子邮件的行为与Web应用程序的状态无关(所有服务都知道是要求将电子邮件发送给这些人的).因此,有一个独立的服务可以处理发送电子邮件是有道理的.

A good example is sending emails. Emails contain data (model). Users have sent Emails (controller). Emails look a certain way (view). However, the act of actually sending emails is independent of the web application's state (all the service knows is it was asked to send this email to these people). Thus, it makes sense that there is an independent service that just handles sending emails.

类似地,生成PDF文件的行为与Web应用程序的状态无关. PDF生成器不需要知道您的应用程序中发生了什么,只知道它被要求制作PDF.

Similarly, the act of generating PDF files is independent of the state of the web application. A PDF generator doesn't need to know about what's going on in your app, it just knows it was asked to make a PDF.

这篇关于Symfony2 MVC:我的代码在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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