在CakePHP 2中抛出自定义503异常 [英] Throwing a custom 503 exception in CakePHP 2
问题描述
我需要提供 503服务不可用 HTTP标头。
通常我使用404和500代码。
NotFoundException();
throw new InternalErrorException();
但我在CakePHP 2.2中找不到503的相关异常?
在我的app / lib / AppExceptionHandler.php文件中,我有一个处理错误的类/函数。
class AppExceptionHandler {
public static function handle($ error){
}
编辑:
-
文件夹我创建了一个名为 MyExceptions.php 的文件。我在这个文件中写了以下代码:
class ServiceUnavailableException extends CakeException {
protected $ _messageTemplate = '服务现在不可用';
}
-
在 bootstrap.php 我这样写:
App :: uses('MyExceptions','Lib'); li>
然后在 MyNewController.php 文件中写入:
throw new ServiceUnavailableException(just trying); / p>
但是,当我抛出此异常时会收到此错误:
致命错误:
中找不到类'ServiceUnavailableException'/var/www/vhosts/example.com/httpdocs/app/Controller/MyNewController.php
on line 3560
警告(2):无法修改标题信息 -
发送的标题(输出开始于
/var/www/vhosts/example.com/httpdocs/ app / Controller / MyNewController.php:3560)
[APP / Lib / AppExceptionHandler.php,第19行]
我错过什么?
谢谢
Cake 2.0书中的例外页面值得一读。
这是我的短测试:
//在bootstrap.php
require 。DS。'MyExceptions.php');
// in /Lib/MyExceptions.php
class ServiceUnavailableException extends CakeException {
protected $ _messageTemplate ='Test';
}
// in controller
throw new ServiceUnavailableException('Service unavailable',503);
I need to give a 503 Service Unavailable HTTP header.
Normally I use these for 404 and 500 codes
throw new NotFoundException();
throw new InternalErrorException();
But I couldn't find relevant exception for 503 in CakePHP 2.2?
In my "app/lib/AppExceptionHandler.php" file I have this class/function that handles errors.
class AppExceptionHandler {
public static function handle($error) {
}
Edit:
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'; }
In "bootstrap.php" I wrote this:
App::uses('MyExceptions', 'Lib');
Then inside "MyNewController.php" file I wrote this:
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]
What do I miss? Thank you
The Exceptions page in the Cake 2.0 book is worth a read.
Here's my short test:
// 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异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!