Yaf如何自定义抛出异常catchException

来源:互联网 发布:淘宝店是怎么进货的 编辑:程序博客网 时间:2024/06/14 20:32

Yaf的抛出分为错误级别和异常级别,我这里速记的是如何自定义异常捕获已经处理


系统默认的抛出通常会暴露你的文件结构以及命名方式,在系统面向公网的时候,我们有必要对这些做出屏蔽或者处理

define("APP_PATH",  __DIR__);$app = new Yaf_Application(APP_PATH . "/conf/application.ini");$app->getDispatcher()->catchException(true);$app->bootstrap()->run();

这样设置也是可以的,至于效率哪个更好,我不是很清楚,懂的童靴麻烦告诉一下

Yaf_Dispatcher::getInstance()->catchException(true);


设置catchException为true,可以自定义捕获

捕获处理比较简单

在当前module下面创建一个Error的Controller下面创建一个errorAction

class ErrorController extends Yaf_Controller_Abstract {    public function errorAction($exception) {        Yaf_Dispatcher::getInstance()->disableView();        /* error occurs */        switch ($exception->getCode()) {            case YAF_ERR_NOTFOUND_MODULE:            case YAF_ERR_NOTFOUND_CONTROLLER:            case YAF_ERR_NOTFOUND_ACTION:            case YAF_ERR_NOTFOUND_VIEW:                echo 404, ":", $exception->getMessage();                break;            default :                $message = $exception->getMessage();                echo 0, ":", $exception->getMessage();                break;        }    }}


用函数设置也比较简单,将抛出由异常转换为错误级别

$app->getDispatcher()->throwException(FALSE);$app->getDispatcher()->setErrorHandler("myErrorHandler");$app->bootstrap()->run();function myErrorHandler($errno, $errstr, $errfile, $errline){    switch ($errno) {        case YAF_ERR_NOTFOUND_CONTROLLER:        case YAF_ERR_NOTFOUND_MODULE:        case YAF_ERR_NOTFOUND_ACTION:            header("Not Found");            break;        default:            echo 'errno: '.$errno.'<br>';            echo 'errstr: '.str_replace(APP_PATH, '[PATH]', $errstr).'<br>';            echo 'errfile: '.str_replace(APP_PATH, '[PATH]', $errfile).'<br>';            echo 'errline: '.$errline.'<br>';            break;    }    return true;}

2 0
原创粉丝点击