【发布时间】:2013-12-02 10:16:11
【问题描述】:
如何在控制器中捕获异常并在 Symfony 2 中显示 flash 消息?
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 块中做什么?
【问题讨论】:
-
toString($e) 不工作。它显示 FatalErrorException: Error: Call to undefined function toString()
-
echo (string)$e;或更好,在高效网站上向您发送电子邮件:mail('you@x.com', 'Exception in script ...', var_export($e, true));