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!

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.

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

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

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.

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.

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.

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.

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


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SublimeText3 Mac version
God-level code editing software (SublimeText3)

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

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 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),
