0

我在 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;
        }
    }
}
4

1 回答 1

1

您应该尝试 catch (\Symfony\Component\HttpKernel\Exception\NotFoundHttpException $e)或仅使用此 NotFoundHttpException ...

可能它想抓住SeerUK\DWright\GalleryBundle\Controller\Symfony\Component\HttpKernel\Exception\NotFoundHttpException你的情况:)

于 2013-03-12T20:57:44.530 回答