Member-only story
Symfony 4: How to move fat logic to Services
One of my recurrent concerns while building MVC applications is to have no extensive logic in the controllers.
That’s why is common to say “Skinny Controllers, Fat Models”.
But is unclear what to do when the model becomes too fat. This can easily happen while mixing structure and business logic. Your models could become too big and complex.
One of the possible solutions is extracting code from the models to services. You’ll get cleaner and better-organized code. The code is easier to maintain.
This way you’ll say instead, “Skinny Models, Skinny Controllers, Fat Services”.
It is a way of managing logic so that you achieve Don’t Repeat Yourself (DRY), avoiding having the same logic in different places, and not having controllers and/or models taking too much responsibility.
Another advantage is that, if you have logic in simple POPO (Plain Old PHP Objects), the unit tests became simpler to make.
In this article, I’m going to share with you, how to achieve that with Symfony 4, step by step. Stay with me.
First Step: Create the Controller
This is what I typically have in the controllers:
- Handle a request and create a…