Situation
I have a controller which handle a page edit for instance.
/**
* Update a page
* @ParamConverter("page", class="AcmeBundle:Page", options={"id" = "page_id"})
*/
public function editAction(Page $page, Request $request)
{
$form = $this->createForm(PageType::class, $page);
if ($form->handleRequest($request)->isSubmitted() && $form->isValid()) {
$this->getDoctrine()->getManager()->flush();
$this->addFlash('success', 'page_admin.flash.updated');
if ($form->get('save_and_stay')->isClicked()) {
return $this->redirect($request->headers->get('referer'));
}
else {
return $this->redirectToRoute('acme_page_admin_index');
}
}
return $this->render('AcmeBundle:PageAdmin:edit.html.twig', array(
'form' => $form->createView()
));
}
My form include two submit buttons, one to save the Page and come back to the index, one to save the Page and stay in the edit form.
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('headline')
->add('body')
->add('save', SubmitType::class)
->add('save_and_stay', SubmitType::class)
;
}
Question
When the form is submitted, I simply check which button has been clicked and perform the right action. But I guess I need to make a service for this and I don't know exactly how. How to name it (acme.form.save_and_stay
?), where to store it (AcmeBundle\Service\SaveAndStay
?).
I have some difficulties to organize my Symfony app with the services and to understand when I am supposed to use them.