从AppExceptionHandler渲染视图 [英] Render View from AppExceptionHandler

查看:169
本文介绍了从AppExceptionHandler渲染视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用CakePHP 2.0并想要处理一个ForbiddenException。
我按照

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

我的异常现在被捕获在AppExceptionHandler,但我不知道如何从这里移动。我想呈现相关的视图,但 $ this 不可用。
任何人都有起点吗?

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?

编辑

我的代码到目前为止到Cookbook示例:


My code so far is identical to the Cookbook example:

在app / Config / core.php中

In app/Config/core.php

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

在app / Config / bootstrap.php

In app/Config/bootstrap.php

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

在app / Lib / AppExecptionHandler.php

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');     
        }
    }
}

Bart

推荐答案

正如您在评论中提到的,您可以将错误视图的副本复制到您自己的视图文件夹并控制渲染。

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

如果您还想使用会话,请记住,在应用程序的任何部分,您可以使用 CakeSession 也可以访问会话。

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

这篇关于从AppExceptionHandler渲染视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆