Search code examples
phpsymfonyrefactoringaction

Symfony 3 - Refactor controller action


I’m working on a Symfony 3 project wher I discovered a very long controller action (like 1k lines and lany different treatments inside) that I want to refactor by cutting it into several other functions.

My controller has already a lot of actions and functions, and I don’t know if it’s a good idea to multiply the functions inside.

What is the cleanest way to refactor this kind of function ? Should I make a service only for this action ?


Solution

  • as @Jonathan says, try to identify repeated code, abstract it to a Service if it will be consumed in others Controllers/Services.

    In symfony 3, you have available repositories, you could try to identify repeated querys on the controller and move it to the repository of the entity.

    Also, if you are app is creating forms in the controller, you could move it to Form clases to reduce code in your controller. Creating form clases

    I hope that helps you to reduce code in your controllerss