Symfony 的 createNotFoundException 不返回 404 页面

Symfony's createNotFoundException not returning 404 page

我有一个 Symfony 操作,我试图在查询 returns NULL 时 return 出现 404 错误。

我总是得到常规页面的模板 returned 和 200 HTTP return 代码。

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

我是 运行 Symfony 2.7.1

知道为什么这段代码没有 return404 页面吗?

<?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);
    }


}

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

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