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

Analysis thinkphp5 shows render incompatibility problem

藏色散人
藏色散人forward
2021-11-29 15:19:492267browse

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')
    }  
}

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')
    }  
}

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!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete