Home > PHP Framework > ThinkPHP > Analysis thinkphp5 shows render incompatibility problem

Analysis thinkphp5 shows render incompatibility problem

藏色散人
Release: 2021-11-29 15:19:49
forward
2274 people have browsed it

The following thinkphp framework tutorial column will introduce to you the render incompatibility issue of TP5 custom global exception handling prompts. I hope it will be helpful to friends in need!

TP5 custom global exception handling, all exceptions thrown are passed automatically Define the render method to render and then return to the client for display.
You need to customize the render method of the handle and overwrite it:

namespace app\lib\exception;  
  
use think\Exception;  
use think\exception\Handle;
class ExceptionHandler extends Handle  
{  
  public function render(Exception $e)  
    {  
        //TODO:
        return json('invalid request')
    }  
}
Copy after login

After that, the postman verification interface appears and the following error message is incompatible:
Analysis thinkphp5 shows render incompatibility problem

Trace the original Handle. php file,
Analysis thinkphp5 shows render incompatibility problem

Check use and find that the source file uses Exception, and I use think\Exception:
Analysis thinkphp5 shows render incompatibility problem

Modify the code:

namespace app\lib\exception;  
  
use Exception;  
use think\exception\Handle;
class ExceptionHandler extends Handle  
{  
  public function render(Exception $e)  
    { 
        //TODO:
        return json('invalid request')
    }  
}
Copy after login

The result is correct:
Analysis thinkphp5 shows render incompatibility problem

The above is the detailed content of Analysis thinkphp5 shows render incompatibility problem. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:segmentfault.com
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