Home > Backend Development > PHP Tutorial > Laravel5.x如何让所有的错误都返回一个json?

Laravel5.x如何让所有的错误都返回一个json?

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-06 20:28:09
Original
879 people have browsed it

比如正常的一个请求,返回方式如下:

return response($result, $code);  // 返回json
Copy after login
Copy after login

api接口返回html不方便看,怎么让任何错误返回json,包括代码错误,数据库连接错误

回复内容:

比如正常的一个请求,返回方式如下:

return response($result, $code);  // 返回json
Copy after login
Copy after login

api接口返回html不方便看,怎么让任何错误返回json,包括代码错误,数据库连接错误

App\Exceptions\Handler 的render方法里,直接return response()->json();

这里面你可以对不同的错误返回不同的json

这里

try{ 
    $conn = new redis();
 }catch (\Exception $e){ 
    var_dump($e);//捕获错误
}
Copy after login
Related labels:
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template