Blogger Information
Blog 38
fans 0
comment 0
visits 30671
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
tp5返回json格式异常
图图的博客
Original
539 people have browsed it

文章摘自:https://blog.csdn.net/chengya1919/article/details/107414412

需求:

tp5默认返回异常的格式是html格式,如果用tp做接口的话返回html前端接收不了,所以要将html格式转化成接送格式;

实现:

  1. 在config/app.php 中直接修改( 需要其它返回异常错误,可以参考 Handle.php)

    'exception_handle'       => function($e) {
       // 异常报错
       return json([
           'code'=>$e->getCode(),
           'msg'=>$e->getMessage(),
           'api_url'=> $_SERVER['REQUEST_URI'],
           'file_line'=> $e->getFile() . '  line:'.$e->getLine(),
       ]);
    },

效果:

随便写点错误代码

public function test(){
   asdf;
}

修改前:微信截图_20220720135048.png

修改后:

微信截图_20220720135236.png

Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments