Home > PHP Framework > YII > body text

yii2 custom error handling

(*-*)浩
Release: 2019-12-14 09:34:20
Original
3279 people have browsed it

yii2 custom error handling

When using Yii2, we found that once an error occurs in the program, Yii2 can automatically display its dedicated error prompt interface, which is completely different from the error prompt interface that appeared when we wrote the original ecology. Same.

How is it done: Where is the monitoring set up? Or where to use try catch? ”.                                                                       (Recommended learning: yii framework)

In fact, PHP has its own dedicated error handling API. When a problem occurs in the program, the specified function can be automatically called. And Yii2 takes advantage of this. , when it starts, use PHP's built-in set_error_handler to register its own error handling and turn off PHP's own error display.

Yii2 Error custom processing

In the official tutorial, it tells us that to enable custom errors, we need to configure the components as follows:

return [
    // ...
    
    'components' => [
        // ...
        
        'errorHandler' => [
            'errorAction' => 'site/error',
        ],
    ]
    
    // ...
];
Copy after login

Yii2 Error core processing method

In Yii2 In error customization processing, we understand the principle of Yii2 error registration and know that after the error occurs, Yii2 will call the handleException and handleError methods of the yii\web\ErrorHandler class object.

In fact, yii\web\ There are no handleException and handleError methods in the ErrorHandler class, but there are in its base class yii\base\ErrorHandler. These two functions will eventually call the renderException method of yii\web\ErrorHandler. The code is as follows:

# yii\web\ErrorHandler
/**
 * Renders the exception.
 * @param \Exception $exception the exception to be rendered.
 */
protected function renderException($exception)
{
    // ...
    // 注意:控制错误是否能自定义的关键
    // !YII_DEBUG 表示了如果你现在是在 Debug 模式下,那么不能自定义错误
    // 即使你配置了组件的 'errorHandler' => ['errorAction' => 'site/error'] 参数
    $useErrorView = $response->format === Response::FORMAT_HTML && (!YII_DEBUG || $exception instanceof UserException);
    // 传递到自定义错误处理方法
    if ($useErrorView && $this->errorAction !== null) {
        $result = Yii::$app->runAction($this->errorAction);
        if ($result instanceof Response) {
            $response = $result;
        } else {
            $response->data = $result;
        }
    }
    
    // ...
}
Copy after login

In The final error display method renderException we can see: customized errors need to be in non-DEBUG mode to take effect

The above is the detailed content of yii2 custom error handling. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!