I'd like to know where to store some custom code which is related to form. I'm writing the Symfony app in which user can add his own category (using form of course). When user add his category the form code inside the controller check if this form is submitted and valid. If yes then user's category and the URI which is creating based on the category name are added to database. Now this whole code and logic is stored in CategoryController inside addCategory() action. Just like the following:
public function addCategory(Request $request): Response
{
// create the whole form inside CategoryType class
$form = $this->createForm(CategoryType::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$categories = $form->getData();
$categories->setName(preg_replace(
'#\s+#', ' ', $categories->getName()));
$categories->setCategoryUri(strtolower(str_replace(
' ', '-', $categories->getName())));
$this->getDoctrine()->getRepository(Categories::class)
->addOneCategory($categories);
return $this->redirectToRoute('flashcard_index');
}
return $this->render('category/add_category.html.twig', [
'form' => $form->createView(),
'slug' => 'Add category'
]);
}
As you see inside the if statement there's code I'm writing about. First, user data are saved to $categories
variable, next I'm removing more than one space using preg_replace()
(in case the user enter more than one space in form field) and finally I create URI based on category name using strtolower()
and str_replace()
functions.
The problem is I don't know if storing this above logic inside a controller action is a good practice and if not, where to store this logic? Could you please answer me on that question? Thank you in advance for all answers!