Render View from AppExceptionHandler

2019-04-10 06:27发布

问题:

I'm working with CakePHP 2.0 and want to handle a ForbiddenException. I've followed the example explained at the CakePHP Cookbook.

My exception is now caught at the AppExceptionHandler but I don't know how to move from here. I want to render a relevant View but $this is not available. Does anyone have a starting point for me?

Edit:
My code so far is identical to the Cookbook example:

In app/Config/core.php

Configure::write('Exception.handler', 'AppExceptionHandler::handle');

In app/Config/bootstrap.php

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

In app/Lib/AppExecptionHandler.php

class AppExceptionHandler {
    public static function handle($error) {
        if($error instanceOf ForbiddenException ){
            echo 'Oh noes! ' . $error->getMessage();
            // $this->Session->setFlash('To access the page please login');     
        }
    }
}

regards, Bart

回答1:

As you mentioned on your comment, you can make copies of the error views to your own View folder and control the rendering.

If you want to use the session as well, remember that, in any part of your application, you can use CakeSession to access the session as well.

http://book.cakephp.org/2.0/en/development/sessions.html#reading-writing-session-data