在CakePHP 2中抛出自定义503异常

时间:2012-11-27 18:24:40

标签: php cakephp exception-handling cakephp-2.0 http-status-code-503

我需要提供 503 Service Unavailable HTTP标头。

通常我将这些用于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]

我想念什么? 谢谢

2 个答案:

答案 0 :(得分:3)

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);

答案 1 :(得分:0)