How does yaf customize to throw an exception catchexception?

Source: Internet
Author: User

Yaf throws are divided into error and exception levels. Here I stenographer how to customize the exception capture that has been handled


The system throws by default usually expose your file structure and naming method. When the system is facing the public network, it is necessary for us to block or handle this.

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

This setting is also acceptable. I am not very clear about which efficiency is better. Please tell me if you know more about the boots.

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


Set catchexception to true, which can be customized

Simple capture Processing

Create an error Controller under the current module and create an 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;        }    }}


Function setting is also relatively simple, and throws are converted from exceptions to error levels.

$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;}

How does yaf customize to throw an exception catchexception?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.