How to avoid Fat Controllers in Symfony
Fat Controller is a term that used when a Controller becomes very large and contains hundreds of lines of code. Best practice is to avoid such a situation or refactor the code to get rid of it.
In this short article, I describe simple technics for having an in shape controller in a Symfony project. These technics may be usable in other frameworks considering their features and structures.
1Use Annotations, it’s good
Variety of Annotations are available in Symfony to help you keep your code clean. Needless to say that you can define custom annotations to handle complex conditions outside the controller, Here are some examples of using annotations.
Some requirements over requests can be done using annotations. For example, consider the following code.
Above code can be refactored to through out some code from the controller.
2Don’t bring validations here
Validations always happen here and there but bringing them inside the controller is not good looking. Symfony handles validation beautifully and you can learn about it from the best source for learning Symfony. Learn and Use Validations.
3Use Before and After Filters
A good part of a controller that can be removed is actually something called a Filter. For example, authenticating a user inside an action is a bad practice but using a before filter to handle authentication cleans up the controller. Learn how to create before and after filters.
4Twig is powerful, Use its power
Twig is a powerful member of a Symfony project. All of us love it because it’s neat and simple. However, twig packed with powerful features. Also, Extensions can simply be developed for it. When writing a controller, keep twig in mind and if anything can be handled inside a template, leave it behind. For example, I wrote an extension for creating image thumbnail of any given size because of a simple reason; It can be off the controller. Learn how to create a twig extension.
As you know, Symfony can handle many things related to forms. All you have to do is to let it!
“The Form component allows you to create, process and reuse forms”
Everything in a Symfony form is customizable from top to bottom.
“Forms are one of the most misused Symfony components due to its vast scope and endless list of features.”
6Put Security inside Seciruty
You know Symfony has a very powerful security component. You are not supposed to deal with security inside your getProducts action. The security component can be customized to load and store user from any source such as your Doctrine Entity or an Auth Service provider. It can handle authorization using simple annotations or complex logic. Just consider reading Symfony Security Guide.
To Sum Up
Fat controllers are dirty and are in violation of best practice. Symfony follows “Thin Controllers” philosophy and prepared enough tools to help us accomplish that. I think the above tips are very useful. What do you think? Help by writing a comment.
(First image in this articles borrowed from philly.com)
I’m Amin and I’ve been using Symfony for more than 6 years.
Please follow me, and don’t forget to leave a comment.
Thank you. eeXBee.com