lundi 23 mars 2015

Symfony2 best practice, business logic and unit test

I'd like to create a highly maintainable code, with unit tests.


I have read the best practices so directories are organized as per best practice, with just one bundle named AppBundle, and I'm using annotations.


I have an issue with business logic. I've read that I should not put business logic inside controllers. As the vast majority of my code was not meant to be reused I put the logic inside the controllers. Yesterday I read here that "logic should live in the controller unless you’re going to unit test it or until you need to re-use it" and this made me faint: am I not going to unit test if I put my logic inside controllers?


I understand that "controllers should be as lean as possible, with few lines of code to glue stuff together". But what about forms? A form often has a logic like "display the form, if it's valid do something and display another page". Now if I'm going to put the form creation and logic inside a service (or a model?) I'd have to put the page render command inside that, so basically the controller would be 1 line, with all the rest inside the service, and the actual decision of which page to display would be done inside the service, not the controller itself...so what's the point of a controller, just the routing?


I really need to understand this before proceeding (i've already developed 3 months on this and I'd have to rework a lot, but better now than never)...


Thank you!


SN


Aucun commentaire:

Enregistrer un commentaire