我需要提供一个 503 Service Unavailable HTTP 标头.
I need to give a 503 Service Unavailable HTTP header.
通常我将这些用于 404 和 500 代码
Normally I use these for 404 and 500 codes
throw new NotFoundException();
throw new InternalErrorException();
但是我在 CakePHP 2.2 中找不到 503 的相关异常?
But I couldn't find relevant exception for 503 in CakePHP 2.2?
在我的app/lib/AppExceptionHandler.php"文件中,我有这个处理错误的类/函数.
In my "app/lib/AppExceptionHandler.php" file I have this class/function that handles errors.
class AppExceptionHandler {
public static function handle($error) {
}
在app/Lib"文件夹中,我创建了一个名为MyExceptions.php"的文件.我在这个文件中写了以下代码:
Inside "app/Lib" folder I created a file named "MyExceptions.php". I wrote the following code inside this file:
class ServiceUnavailableException extends CakeException {
protected $_messageTemplate = 'Service is not available now';
}
在bootstrap.php"中我这样写:
App::uses('MyExceptions', 'Lib');
App::uses('MyExceptions', 'Lib');
然后在MyNewController.php"文件中我写了这个:
Then inside "MyNewController.php" file I wrote this:
throw new ServiceUnavailableException("just try");
throw new ServiceUnavailableException("just trying");
但是当我抛出这个异常时我得到这个错误:
But then I get this error when I throw this exception:
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]
我想念什么?谢谢
Cake 2.0 书中的 Exceptions 页面值得一读.
这是我的简短测试:
// 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);
这篇关于在 CakePHP 2 中抛出自定义 503 异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!