我在 Symfony2 控制器中遇到异常问题...我正在尝试捕获NotFoundHttpException
,但是 catch 块...只是没有发生,它转到带有堆栈跟踪的标准 Symfony2 异常页面在开发环境...
我有以下代码:
<?php
namespace SeerUK\DWright\GalleryBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use SeerUK\DWright\GalleryBundle\Entity\Gallery;
class GalleryController extends Controller
{
public function indexAction($galleryId)
{
try
{
$gallery = $this->getDoctrine()
->getRepository('SeerUKDWrightGalleryBundle:Gallery')
->find($galleryId);
throw $this->createNotFoundException('rawr'); // Just for the sake of testing...
if (!$gallery) {
throw $this->createNotFoundException(
'No gallery found for id ' . $galleryId
);
}
$galleryId = $gallery->getId();
$galleryName = $gallery->getName();
$galleryDesc = $gallery->getDesc();
$galleryPublishedOn = $gallery->getPublishedOn();
return $this->render('SeerUKDWrightGalleryBundle:Gallery:index.html.twig', array(
'galleryId' => $galleryId,
'galleryName' => $galleryName,
'galleryDesc' => $galleryDesc,
'galleryPublishedOn' => $galleryPublishedOn->format('Y-m-d H:i:s'),
));
}
catch (Symfony\Component\HttpKernel\Exception\NotFoundHttpException $e)
{
echo $e->message;
}
}
}