Render View from AppExceptionHandler

2019-04-10 06:01发布

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条回答
The star\"
2楼-- · 2019-04-10 06:27

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

查看更多
登录 后发表回答