dsa99349 2017-04-27 19:14
浏览 71


I'm currently doing a custom ErrorHandler for an App in CakePHP. The reason? Well, bots are always trying to find stuff in your servers and sometimes they provoke exceptions and or errors.

The idea with this ErrorHandler is to filter requests and respond with the appropriate headers and prevent further request damage by handling this type of requests and make it transparent for the user client (because it might affect JavaScript for instance).

And what better way than to use the Framework's functionality, right?

The thing is that since this ErrorHandler is being used statically, well, there is no constructor so nothing inherits anything, it doesn't matter if you instantiate any other CakePHP Object.

What would be the appropriate way to use CakeResponse Object?

CakePHP's configuration:


App::uses('CustomErrorHandler', 'Lib');


// Error and exception handlers.
Configure::write('Error', array(
    'handler' => 'CustomErrorHandler::handleError',
    'level' => E_ALL & ~E_DEPRECATED,
    'trace' => true
Configure::write('Exception', array(
    'handler' => 'CustomErrorHandler::handleException',
    'renderer' => 'ExceptionRenderer',
    'log' => true


  ... rest of class code ...

   * Named after convention: This method receives all CakePHP's
   * errors and exceptions…
   * @param  array $e The exception object.
   * @return mixed    Returns the error handling or header redirection.
   public static function handleException($e)
       $message = (string) $e->getMessage();
       $code    = (int)    $e->getCode();
       $file    = (string) $e->getFile();
       $line    = (string) $e->getLine();

       // If it's a Blacklist resource exception it will log it and redirect to home.
       if (self::__isResourceException($message))
           return self::__dismissError();

       return parent::handleException($e);

   * This method redirects to home address using CakeResponse Object.
   * @return mixed
   private static function __dismissError()
       return (new CakeResponse)->header(array(
           'Location' => self::$redirectUrl


Will try a small layer over the ExceptionRenderer.

  • 写回答

1条回答 默认 最新

  • dpr26232 2017-04-27 22:51

    There's not really a point in using a CakeResponse object there... it would work if you'd call send() on it, however with only that one header, there's no advantage over using header() directly.

    That being said, you are however in any case ditching the Controller.shutdown and Dispatcher.afterDispatch events. They are being dispatched in ExceptionRenderer::_shutdown(), and are often used to set response headers (CORS related headers are a good example), so you should figure whether its OK to drop them, or maybe even required.

    If you need to keep the shutdown and afterDispatch events, then you should either fire them on your own, or maybe even use an extended ExceptionRenderer that handles that specific type of exception and sends an empty reponse with your location header added.

    See also

    本回答被题主选为最佳回答 , 对您是否有帮助呢?



  • ¥15 数学建模——参会安排怎么做
  • ¥15 电脑键盘实现触摸功能
  • ¥25 matlab无法将表达式转换为双数组怎么解决?
  • ¥15 单片机汇编语言相关程序
  • ¥20 家用射频美容仪技术规格
  • ¥15 大家帮我看看为什么错了
  • ¥15 unity互动琴弦抖动效果
  • ¥15 做了个的二极管反向饱和电流测量电路,但是测试达不到效果
  • ¥15 nginx无证书访问https失败
  • ¥15 树莓派启动AP热点传入数据