Symfony2 - 未在控制器中运行catch块

时间:2013-03-12 20:41:58

标签: php exception symfony

我的Symfony2控制器出现异常问题...我正试图抓住NotFoundHttpException,但抓住了......只是没有发生,它符合标准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;
        }
    }
}

1 个答案:

答案 0 :(得分:1)

你应该试试 catch (\Symfony\Component\HttpKernel\Exception\NotFoundHttpException $e)或只使用此NotFoundHttpException ...

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