如何在Symfony 2中捕获控制器异常并显示闪存消息?
try{
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
$em->flush();
return $this->redirect($this->generateUrl('target page'));
} catch(\Exception $e){
// What to do in this part???
}
return $this->render('MyTestBundle:Article:new.html.twig', array(
'entity' => $entity,
'form' => $form->createView(),
));
在catch
块中应该做什么?
echo (string)$e;
或者更好的方式是,在生产环境中发送电子邮件:mail('you@x.com', '脚本异常...', var_export($e, true));
- Daniel W.