如何在 Symfony 2 中模拟 500 错误 [英] How to Simulate 500 Error in Symfony 2
问题描述
我想知道如何在 Symfony 2 中模拟 500 错误.
I am wondering how would I simulate a 500 error in Symfony 2.
我一直在阅读这篇博文,其中Raise建议抛出异常
I have been reading this post where Raise suggests throwing an exception
throw new sfException('测试 500 错误');
throw new sfException('Testing the 500 error');
在 Symfony 1.4 中.
in Symfony 1.4.
我一直把这段代码放在我的
I have been placing this code in my
\store\vendor\symfony\symfony\src\Symfony\Bundle\TwigBundle\Controller\ExceptionController.php
但我得到了致命错误
在第 49 行的/home/notroot/www/store/vendor/symfony/symfony/src/Symfony/Bundle/TwigBundle/Controller/ExceptionController.php 中找不到类Symfony\Bundle\TwigBundle\Controller\sfException"`
Class 'Symfony\Bundle\TwigBundle\Controller\sfException' not found in /home/notroot/www/store/vendor/symfony/symfony/src/Symfony/Bundle/TwigBundle/Controller/ExceptionController.php on line 49`
第 49 行是我添加的异常代码.
Line 49 refers to the exception code I added.
我的问题是在 Symfony 2 中抛出异常是否仍然可以强制出现 500 错误,如果是这样,我应该把这个异常放在哪里?
My question is if throwing an exception is still viable in forcing a 500 error in Symfony 2, and if so where do I put this exception?
如果这不再可行,我将如何测试错误 500?
If this is no longer viable, how would I be able to test for an error 500?
推荐答案
你可以这样做.
//in your controller
$response = new Response();
$response->setStatusCode(500);
return $response;
不要忘记添加
use Symfony\Component\HttpFoundation\Response;
在文件的顶部.
要强制 Symfony 500 错误,您的提议很好:
Edit : To force Symfony 500 error, your proposition is fine :
throw new \Exception('Something went wrong!');
把它放在一个控制器函数中.
Put it in a controller function.
这篇关于如何在 Symfony 2 中模拟 500 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!