Render View from AppExceptionHandler

时光怂恿深爱的人放手 提交于 2020-01-13 03:44:09

问题


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



来源:https://stackoverflow.com/questions/8644352/render-view-from-appexceptionhandler

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!