我需要提供 503 Service Unavailable HTTP header 。
通常我将它们用于 404 和 500 代码
throw new NotFoundException();
throw new InternalErrorException();
但是我在 CakePHP 2.2 中找不到 503 的相关异常?
在我的“app/lib/AppExceptionHandler.php”文件中,我有这个处理错误的类/函数。
class AppExceptionHandler {
public static function handle($error) {
}
编辑:
在“app/Lib”文件夹中,我创建了一个名为“MyExceptions.php”的文件。我在这个文件中编写了以下代码:
class ServiceUnavailableException extends CakeException { protected $_messageTemplate = 'Service is not available now'; }
在“bootstrap.php”中我写道:
App::uses('MyExceptions', 'Lib');
然后在“MyNewController.php”文件中我写道:
抛出新的ServiceUnavailableException(“只是尝试”);
但是当我抛出此异常时,我收到此错误:
Fatal error: Class 'ServiceUnavailableException' not found in
/var/www/vhosts/example.com/httpdocs/app/Controller/MyNewController.php
on line 3560
Warning (2): Cannot modify header information - headers already sent by
(output started at
/var/www/vhosts/example.com/httpdocs/app/Controller/MyNewController.php:3560)
[APP/Lib/AppExceptionHandler.php, line 19]
我想念什么? 谢谢
最佳答案
The Exceptions page in the Cake 2.0 book值得一读。
这是我的简短测试:
// in bootstrap.php
require(APP . 'Lib' . DS . 'MyExceptions.php');
// in /Lib/MyExceptions.php
class ServiceUnavailableException extends CakeException {
protected $_messageTemplate = 'Test';
}
// in controller
throw new ServiceUnavailableException('Service unavailable', 503);
关于php - 在 CakePHP 2 中抛出自定义 503 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13590614/