I have an addAction like this:
public function addAction(Request $request)
{
$object = new Object();
$object->setAttrib('foo');
if($object->isValid())
{
$session->set('_object', $object);
return $this->redirect('confirmAction');
}
}
and in the confirmAction:
public function confirmAction($confirm = 'not_confirmed')
{
if($confirm == 'confirm')
{
$object = $session->get('_object');
if($object->isValid())
{
$entityManager->persist($object);
$session->remove('_object');
return $this->redirect('listAction');
}
}
$this->renderTemplate('with confirm link');
}
I dont like the $session->set part. What is the best practice for this create/confirm/persist things?
Well, you should ask yourself whether or not you want to use JavaScript. A confirmation box would be in my opinion the most sensible solution, since the client doesn’t make another round-trip to the server.
If for some reason you don’t want to use JavaScript, your method is the way to go. Instead of using
$session->set('...');, you could use$session->setFlash('...'), which will store the value in the session for only one request and deletes it afterward.If a user decides not to confirm the action, you could use
$session->setFlash('...')yet again to display the previous state of a form. Using this method is better, because you won’t be having any leftover session values just hanging there.