Pat*_*ick 1 http-status-code-404 symfony
我有一个 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);
}
}
Run Code Online (Sandbox Code Playgroud)

你不需要返回$this->createNotFoundException('Support Group does not exist');而是抛出它:
throw $this->createNotFoundException('Support Group does not exist');
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4549 次 |
| 最近记录: |