G
G
gitdev2020-12-28 22:32:02
symfony
gitdev, 2020-12-28 22:32:02

What should be put into services when working with Symfony?

For example this code. What should be taken out of this code to the service

public function new(Request $request): Response
    {
        $vacancy = new Vacancy();
        $form = $this->createForm(VacancyType::class, $vacancy);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $entityManager = $this->getDoctrine()->getManager();
            $vacancy->setUser($this->getUser());
            $entityManager->persist($vacancy);
            $entityManager->flush();

            return $this->redirectToRoute('vacancy_index');
        }

        return $this->render('vacancy/new.html.twig', [
            'vacancy' => $vacancy,
            'form' => $form->createView(),
        ]);
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis, 2020-12-28
@sidni

In general, too small an example to advise something
1) A form that directly works with an entity is a bad idea that requires you to write unnecessary getters and setters of the entity, it is difficult to check the invariants of the entity and there is a high probability of making the entity object not valid, which is contrary to good principles
2) make a service creating entities like a factory (in general, if you are too lazy to bother, you can push this thing to the repository)
3) The user's network for the vacancy can also be moved to a separate service
4) you can also save the vacancy in the repository
5) the flash can also be called from a separate service (Flusher)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question