Yii provides a complete error handling mechanism based on PHP5 exception handling. When an application starts to run and processes user requests, the handleError method is registered to process PHPwarnings and notices information...
Yii provides a complete error handling mechanism based on PHP5 exception handling. When an application starts to run and processes user requests, it registers the handleError method to process PHP warnings and notices information. it also registers the handleException method to handle uncaptured PHP exceptions. Therefore, if a PHP warning/notice or an uncaptured PHP exception occurs during application running, the error processor takes control of the exception to run the necessary processing mechanism.
Tip:The error processor registration is performed in the constructor method in the application. the set_exception_handler and set_error_handler functions are used. If you do not want Yii to handle errors and exceptions, you can defineYII_ENABLE_ERROR_HANDLER
AndYII_ENABLE_EXCEPTION_HANDLER
False.
By default, when an onError event (or onException event) is triggered, errorHandler (or exceptionHandler) is triggered. If the error or exception is not handled by any event, you need to run the errorHandler component to handle it.
1. exception
The exception thrown in Yii is no different from that in a common PHP file. You can use the following code to throw an exception:
Throw new ExceptionClass ('error information ');
Yii defines two exception classes: CException and CHttpException. The former is a common exception class, and the latter is used to display exception information to end users. At the same time, the latter has a statusCode attribute to represent the HTTP status code. The exception type determines the display effect.
Tip:If you want to tell the user that an operation is incorrect, it is the easiest way to trigger a CHttpException. For example, if you provide 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. display error
When an error is forwarded to the CErrorHandler component, it selects an appropriate view to display the error. If this error is to be displayed to the end user (for example, a CHttpException ),errorXXX
To display errors. ThisXXX
Indicates the HTTP error code (such as 400,404,500 ). If this is an internal error, the View name isexception
. In the latter method, the complete call stack information and error line information are displayed.
Information:When an application runs in production mode, view is used for all errors, including internal errors.errorXXX
. This is because the stack information and error line information of the call may contain some sensitive information. In this case, the developer should rely on the error log to determine the cause of the error.
CErrorHandler searches for appropriate views to display error information. the search order is as follows:
WebRoot/themes/ThemeName/views/system
: In the current topic viewsystem
Directory.
WebRoot/protected/views/system
: In the default view of the applicationsystem
Directory.
yii/framework/views
: In the standard view directory provided by Yii.
Therefore, if you want to display custom errors, you can directlysystem
View directory or topicsystem
Create a view file in the view directory. Each view file is a common PHP file that contains many HTML code. Reference Frameworkview
For more information.
3. use an action to handle errors
Yii can also use controller actions to handle error display. The implementation method is to configure an error processor in the application configuration file.
return array( ...... 'components'=>array( 'errorHandler'=>array( 'errorAction'=>'site/error', ), ),);
In the above code, we configured the CErrorHandler: errorAction attribute, and the attribute value is a routesite/error
. This route pointsSiteController
Inerror
. You can also use other routes.
We can writeerror
Action:
public function actionError(){ if($error=Yii::app()->errorHandler->error) $this->render('error', $error);}
In this action, obtain detailed error information from CErrorHandler: error. If the obtained information is not empty, use the information returned by CErrorHandler: error to render it.error
View. CErrorHandler: error returns an array with the following structure:
code
: HTTP status code (such as 403,500 );
type
: Error type (such as CHttpException,PHP Error
);
message
: Error message;
file
: The PHP file name is incorrect;
line
: The row where the error is located;
trace
: Incorrect call stack information;
source
: Context of the error code.
Tip:The reason for checking whether CErrorHandler: error is null iserror
Actions can be accessed by users. at this time, there may be no errors. When we pass$error
Array to the view, which will be automatically released as independent variables. Therefore, we can use$code
,$type
To access this information.
4. message record
Oneerror
Level error information is recorded when an error occurs. If this error is caused by PHP warning or notice, the message will be recorded inphp
In this category, if the error message is caused by an uncaptured exception, the category will beexception.ExceptionClassName
(For CHttpException, its statusCode will also be appended to the category name ). Developers can use these records to monitor error messages during application execution.
The above is the Yii Framework official guide series 46 -- special topic: Error handling content. For more information, see PHP Chinese website (www.php1.cn )!