search
HomePHP FrameworkYIIyii2 custom error handling
yii2 custom error handlingDec 14, 2019 am 09:34 AM
yii2

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',
        ],
    ]
    
    // ...
];

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;
        }
    }
    
    // ...
}

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!

Statement
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
What Are the Best Practices for Using Yii in a Cloud-Native Environment?What Are the Best Practices for Using Yii in a Cloud-Native Environment?Mar 18, 2025 pm 04:39 PM

The article discusses best practices for deploying Yii applications in cloud-native environments, focusing on scalability, reliability, and efficiency through containerization, orchestration, and security measures.

What Are the Key Considerations for Using Yii in a Serverless Architecture?What Are the Key Considerations for Using Yii in a Serverless Architecture?Mar 18, 2025 pm 04:33 PM

The article discusses key considerations for using Yii in serverless architectures, focusing on statelessness, cold starts, function size, database interactions, security, and monitoring. It also covers optimization strategies and potential integrati

How to Implement Real-Time Data Synchronization with Yii and WebSockets?How to Implement Real-Time Data Synchronization with Yii and WebSockets?Mar 18, 2025 pm 04:34 PM

The article discusses implementing real-time data synchronization using Yii and WebSockets, covering setup, integration, and best practices for performance and security.

What Are the Key Considerations for Deploying Yii Applications in Production?What Are the Key Considerations for Deploying Yii Applications in Production?Mar 17, 2025 pm 01:58 PM

The article discusses key considerations for deploying Yii applications in production, focusing on environment setup, configuration management, performance optimization, security, logging, monitoring, deployment strategies, and backup/recovery plans.

What Are the Key Features of Yii's Built-in Testing Framework?What Are the Key Features of Yii's Built-in Testing Framework?Mar 18, 2025 pm 04:41 PM

Yii's built-in testing framework enhances application testing with features like PHPUnit integration, fixture management, and support for various test types, improving code quality and development practices.

What Are the Key Benefits of Using Yii for Building SaaS Applications?What Are the Key Benefits of Using Yii for Building SaaS Applications?Mar 18, 2025 pm 04:25 PM

The article discusses Yii's benefits for SaaS development, focusing on performance, security, and rapid development features to enhance scalability and reduce time-to-market.

What Are the Best Strategies for Testing Yii Applications with Codeception?What Are the Best Strategies for Testing Yii Applications with Codeception?Mar 18, 2025 pm 04:27 PM

The article discusses strategies for testing Yii applications using Codeception, focusing on using built-in modules, BDD, different test types, mocking, CI integration, and code coverage.

How to Implement Service Discovery and Load Balancing in Yii Microservices?How to Implement Service Discovery and Load Balancing in Yii Microservices?Mar 18, 2025 pm 04:30 PM

The article discusses implementing service discovery and load balancing in Yii microservices, detailing steps and best practices for efficient communication and workload distribution.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),