【问题标题】:Symfony2 - Not running catch block in controllerSymfony2 - 不在控制器中运行 catch 块
【发布时间】:2013-03-12 20:41:58
【问题描述】:

我的 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;
        }
    }
}

【问题讨论】:

    标签: php exception symfony


    【解决方案1】:

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

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

    【讨论】:

    • 您先生,您说得对!它只需要在 Symfony2 异常名称之前的 `` :)
    猜你喜欢
    • 1970-01-01
    • 2022-06-10
    • 2019-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-19
    • 1970-01-01
    • 2016-01-30
    相关资源
    最近更新 更多