symfony / symfony-docs

The Symfony documentation
https://symfony.com/doc
Other
2.18k stars 5.12k forks source link

[Best practices] Bad practice introduced within How the Controller Looks #4472

Closed skafandri closed 9 years ago

skafandri commented 9 years ago

I was browsing the documentation about Best practices where I found a lot of good suggestions. however, please check this out:

http://symfony.com/doc/current/best_practices/controllers.html#how-the-controller-looks Calling a repository from a controller is far from being a good practice. Is rather a bad practice.

Here is a real case putting emphasis on this issue: We were working on a MySQL based Symfony application and we had to switch some of the entities to MongoDb to increase performance. We had to create a new EntityManager and to change the namespace of the ported entities. Hundreds of actions in controllers had to be updated to reference the new EntityManagers. It was a tedious work. Of course, coming accross, we removed the calls to EntityManagers from controllers and moved that logic to services. Next time we decide to switch to Reddis or whatever, we will need to just update few services, not dozens of controllers and hundreds of actions.

We may think about rephrasing the paragraph I added, but I think is important to present a sample Controller code that respects good practices.

linaori commented 9 years ago

I think it's also a matter of how to fetch it. I prefer to define my Repositories as a service and just inject them in the constructor, that way you have non of those issues (if you interface it).

timglabisch commented 9 years ago

i think @skafandri is right here. but may an alternative would describing some architecture styles you can use on top of symfony. event sourcing, ddd stuff, hexagonal, lambda, ....

wouterj commented 9 years ago

I'm going to close this issue and it's connected PR. I can see your point, however:

Thank you for proofreading the guide and trying to improve it though! If you don't agree or if you have found a better, easier, way to solve this problem, you can always comment (github does have a reopen button).