Symfony的createNotFoundException没有返回404页面

时间:2015-06-25 13:33:01

标签: symfony http-status-code-404

我有一个Symfony操作,我在尝试在查询返回NULL时返回404错误。

我总是返回常规页面的模板和200 HTTP返回码。

我已经检查过,我的错误日志显示createNotFoundException正在触发。

我正在运行Symfony 2.7.1

为什么此代码没有返回404页面的任何想法?

<?php

namespace Example\GroupBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Symfony\Component\HttpFoundation\Request;

use Example\GroupBundle\Entity\Group;

/**
 * Class SupportGroupLandingController
 * @package Example\GroupBundle\Controller
 *
 * @Route("/group")
 */
class SupportGroupController extends Controller
{
    /**
     * @Route("/{name}", name="support_group_page")
     * @Method("GET")
     * @Template("ExampleGroupBundle::group_page.html.twig")
     *
     * @param $name
     * @return array
     */
    public function indexAction($name)
    {
        $repo = $this->getDoctrine()->getRepository('ExampleGroupBundle:Group');
        $group = $repo->findOneBy(array('name' => $name));

        if ($group === NULL) {
            error_log('group is null');

            return $this->createNotFoundException('Support Group does not exist');

            error_log('this should not be here');

        } else {
            error_log('group is not null: '.var_export($group, TRUE));
        }

        return array('group' => $group);
    }


}

Symfony debug toolbar

1 个答案:

答案 0 :(得分:3)

你不需要返回$this->createNotFoundException('Support Group does not exist');但是扔掉它:

throw $this->createNotFoundException('Support Group does not exist');