2
votes

I had been wondering why my error page caused certain pages of my site not to render, but then I realized that it's because AppError extends ErrorHandler instead of AppController. This caused some variables that I set in AppController's beforeFilter method not to be sent to the view. Since I can't access session variables from AppError, I thought that I might be able to get away with using the classRegistry to instantiate something that could and simply copying and pasting the rest of my code from AppController's beforeFilter... but that isn't working, nor does it seem like a very elegant fix. Does anyone have any clues as to what would be the best way to approach this? Thanks, David.

2

2 Answers

6
votes

Your AppError class has a controller instance. You can call the beforeFilter manually:

<?php
class AppError extends ErrorHandler {
  function error404() {
    $this->controller->beforeFilter();
    parent::error404();
  }
}
?>
1
votes

In CakePHP 2, you can do something like this to achieve the same effect. In app/Config/bootstrap.php, add this line:

Configure::write('Exception.renderer', 'AppExceptionRenderer');

Then create a file app/Lib/Error/AppExceptionRenderer.php with this code:

App::uses('ExceptionRenderer', 'Error');

class AppExceptionRenderer extends ExceptionRenderer {
    protected function _outputMessage($template) {
        $this->controller->beforeFilter();
        $this->controller->render($template);
        $this->controller->afterFilter();
        $this->controller->response->send();
    }
}

Described more generally here: http://book.cakephp.org/2.0/en/development/exceptions.html#using-a-custom-renderer-with-exception-renderer-to-handle-application-exceptions

Edit: Updated link to point to correct location of the CakePHP 2.0 Book as of July 05, 2012.