問題描述
我需要提供一個 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模板網!