Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code separation in symfony 2 - Controller vs Service vs entity

Tags:

I am using symfony 2 and I have question about code separation. I would like to make sure that I correctly understand what elements should be in a controller, what in a service and what in a entity.

Let's imagine that I have list of documents that I need to display. On each document before displaying I have to also perform some logic operation (e.g. add two variables).

As in understand entity class takes care only on data retrieval and operation on single entity. I should not input there any custom code. As I understand this should be done by a service.

But should I:

  • use a service to pass to controller list of documents based on some
    criteria after performing the required logic,
  • or use a controller to download list of documents, and than pass document to service to perform some logic?

I would rather think that the first approach is appropriate to keep controller thin (thin controllers, big models) but is this approach right? What code should be in entity, what in controller and what in a service?

In particular where should I relate to entity manager - in a controller or rather in service?

Let's also pretend that in many place in my app I need to check if document is finalized before allowing user to perform any action (e.g. edit it). This definitely should be either in a service, as another service would be required to check this. Should I however load the document entity object in controller, send it to service to verify whether it may be finalized or rather load document in service and there perform a check?