Yii Framework Official Guide Series 46--topic: Error Handling

Source: Internet
Author: User



Yii provides a complete, error-handling mechanism based on PHP5 exception handling. The HandleError method is registered to handle PHP warnings and notices information when an application starts running and the user requests are processed, and the HandleException method is also registered to handle the uncaught PHP exception. Therefore, if a PHP warning/notice or an uncaught PHP exception occurs during the application run, the error handler takes control to run the necessary processing mechanism.

tip: The registration of the error handler is done in the constructor method in the application, using PHP functions Set_exception_handler and Set_error_handler. If you do not want Yii to handle errors and exceptions, you can define and be false in the portal file YII_ENABLE_ERROR_HANDLER YII_ENABLE_EXCEPTION_HANDLER .

By default, ErrorHandler (or Exceptionhandler) is triggered when the OnError event (or Onexception event) is triggered. If the error or exception is not handled by any event, then you need to run the ErrorHandler component to process it.

1. Throwing an exception

Throwing exceptions in Yii is no different from normal PHP files. You can use the following code to throw an exception:


throw new Exceptionclass (' error message ');

Yii defines two exception classes: CException and chttpexception. The former is a generic exception class, and the latter is used to display exception information to the end user. At the same time, the latter has a statuscode attribute to represent the HTTP status code. The type of exception determines the display effect, which is discussed below.

tip: to tell the user that an action is wrong, it is easiest to throw a chttpexception exception. For example, if the user provides an invalid ID value in the URL, we can display a 404 error:


If the submitted ID is invalid, throw new Chttpexception (404, ' This page does not exist ');

2. Error display

When an error is forwarded to the component Cerrorhandler, it chooses the appropriate view to display the error. If the error is to be displayed to the end user (for example, a chttpexception), a view named is used errorXXX to display the error. This XXX represents the HTTP error code (for example, 400,404,500, etc.). If this is an internal error, it should only be seen by the developer, then the view name that will be used is exception . In the latter, the complete call stack information and error line information will be displayed.

Info: When the app is running in production mode, all errors, including internal errors, will be used in the view errorXXX . This is because the stack information that is called and the error line information may contain some sensitive information. In this case, the developer should rely on the error log to determine the cause of the error.

Cerrorhandler will search for the appropriate view to display the error message in the following order:

    1. WebRoot/themes/ThemeName/views/system: In the directory under the current theme view system .

    2. WebRoot/protected/views/system: In the catalog of the default view of the app system .

    3. yii/framework/views: In the Standard view directory provided by Yii.

Therefore, if you want to customize the error display, you can system create a view file directly in the view directory or in the theme's system view directory. Each view file is a regular PHP file that contains many HTML code. For more information, refer to the file under the framework's view directory.

3. Use an action to handle errors

Yii can also use controller actions to handle error displays. The implementation method is to configure an error handler in the application's configuration file.


Return Array (...    ') Components ' =>array (        ' ErrorHandler ' =>array (            ' erroraction ' = ' site/error ',        ),)    ;

In the above code, we configured the Cerrorhandler::erroraction property, and the property value is a route site/error . This route points SiteController to the error . Of course, you can also use other routes.

We can write actions like this error :


Public Function Actionerror () {    if ($error =yii::app ()->errorhandler->error)        $this->render (' ERROR ' , $error);}

In this action, we first get the detailed error message from the Cerrorhandler::error. If the information obtained is not empty, the view is rendered using the information returned by Cerrorhandler::error error . The information returned by Cerrorhandler::error is an array with the following structure:

    • code: HTTP status code (e.g. 403, 500);

    • type: Error type (e.g. chttpexception, PHP Error );

    • message: Error message;

    • file: The PHP file name where the error occurred;

    • line: The line where the error occurred;

    • trace: Incorrect call stack information;

    • source: The context of the code where the error occurred.

Tip: We check if the cerrorhandler::error is empty because the error action can be accessed by the user, and there may be no error. When we pass $error an array to the view, it will be automatically released as a separate variable. So, in the view we can use $code , $type to access this information.

4. Message Logging

A error level of error information is logged at the time the error occurs. If this error is caused by PHP warning or notice, then the message will be recorded in php this category, and if the error message is caused by an uncaught exception, then the classification will be exception.ExceptionClassName (for Chttpexception, Its statuscode will also be appended to the category name). Developers can use these records to monitor error messages at the time of application execution

The above is the Yii Framework Official Guide Series 46--topic: Error handling content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • 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.