I'm developing a small CMS for my company using Symfony2. I really love this framework. I love form classes and reusing them (that's all about forms after all).
But (yes, there is a "but") I'm feeling like I'm doing the same stuff, copy and paste in all controllers. A code duplication that we hate. With all the business logic moved to Services and forms, events, persist actions in Doctrine, all my controllers do the same thing:
$this->get('mycompany.repository.entity')
What I mean is that controller actions are all the same, just a few characters changes.
How can be DRY with Symfony2 in my controllers? Maybe controller as service (a concept that I discovered only a few days ago) may help?
EDIT: related question (to the first idea suggested by Boo): Symfony2 how to redirect to an action without hardcoding route name?
There are many ways to get to a more DRY code base:
I guess there are even more ways when thinking about it.
I find idea 1 very elegant while 3 also have some advantages. The second approach is kind of blury, especially as you again need dublicated code (even if it will be much less, it's far from optimal). Idea 1 is in sync with the OO idea and it communicates it's intent well. To sum it up, I would use a BaseController!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With