symfony2 返回404响应
示例
当在服务器上找不到资源时,将返回404响应。在Symfony中,可以通过引发NotFoundHttpException异常来创建此状态。为了避免use在控制器内部添加多余的语句,请使用类createNotFoundException()提供的Controller
<?php namespace Bundle\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; class TestController extends Controller { /** * @Route("/{id}", name="test") * Recommended to avoid template() as it has a lot of background processing. * Query database for 'test' record with 'id' using param converters. */ public function testAction(Test $test) { if (!$test) { throw $this->createNotFoundException('Test record not found.'); } return $this->render('::Test/test.html.twig', array('test' => $test)); } }